In: Computer Science
Question 1: Write a program in C++ using multi filing which means 3 files ( main file, header file, and functions file) and attached screenshots as well. Attempt the Question completely which contain a and b parts
a) Write a program that takes a number from the user and checks whether the number entered validates the given format: “0322-5441576”, xxxx-xxxxxxx where “x” implies the digits only and first two digits are 0 and 3 respectively. The program also identifies the Network of the given number. (Use of string related functions is a must)
b) Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions.
For character identification
Develop a program that takes a string argument, and returns an array containing all unique characters.
For character counting
Develop a program that takes an array returned from above function as an argument along with the given string and return an array containing the total count of each uniquely identified character present in the argument array.
Note: kindly write a proper code of program mention in Question and answer complete Question along with both parts and screenshots Thanks.....
a)
// // a) Write a program that takes a number from the user
// and checks whether the number entered validates the given format: “0322-5441576”,
// xxxx-xxxxxxx where “x” implies the digits only and first two
// digits are 0 and 3 respectively.
// The program also identifies the Network of the given number.
// (Use of string related functions is a must)
#include <iostream>
#include <string>
using namespace std;
int main(){
string number = "";
cout << "Please enter the number: ";
cin >> number;
if(number[0] != '0' || number[1] != '3' || number.size() != 12 ||
number[4]!= '-'){
cout << "Not in the format 03xx-xxxxxxx, Exiting..."<< endl;
exit(1);
}
else{
cout << "Number is in the required format!\n";
}
return 0;
}
b)
/*
Write a program that takes a string from the user, identifies and counts
all unique characters in that given string. You are bound to use only
built-in string functions where necessary. For identification of unique
characters and for counting of the characters make separate functions.
For character identification
Develop a program that takes a string argument, and returns an array containing all unique characters.
For character counting
Develop a program that takes an array returned from above function as an argument along with the given string and return an array containing the total count of each uniquely identified character present in the argument array.
*/
#include <bits/stdc++.h>
using namespace std;
vector <char> getAllUniqueChars(string& s){
unordered_map <char,bool> umap;
for(auto x:s){
if(umap.find(x) != umap.end())
umap[x] = 1;
}
vector <char> uniqueChars;
for(auto x:umap)
uniqueChars.push_back(x.first);
return uniqueChars;
}
unordered_map <char,int> getCountOfChars(vector <char>& uniquechars,
string input_string) {
unordered_map <char,int> umap;
for(auto x:input_string)
umap[x]++;
return umap;
}
int main() {
string input_string = "";
getline(cin, input_string);
vector <char> uniquechars = getAllUniqueChars(input_string);
unordered_map <char, int> countArray = getCountOfChars(uniquechars, input_string);
for(auto x:countArray)
cout << x.first << " --> " << x.second << endl;
return 0;
}