C++ CODING!!! Write a function, void count(char input[], int &lower, int &upper,
ID: 3774197 • Letter: C
Question
C++ CODING!!!
Write a function, void count(char input[], int &lower, int &upper, int &digits, int &symbols), that takes as parameter a character array, which may contains lowercase, uppercase, digits, and other special symbols (e.g, *, $, ...), and outputs the number of lowercase, uppercase, digits, and special symbol. Please use a character array to store the input string. You can assume the character array is large enough to store all input characters. Also, write a program to test your function.
Example input: AaBbCc123!@#
Output: Lowercase: 3, Uppercase: 3, Digits: 3, Special symbol: 3.
Grading scheme: a program reads a string and calls the function correctly - 5 points; counting each cases correctly - 15 points.
Explanation / Answer
#include <iostream>
using namespace std;
void count(char input[], int &lower, int &upper, int &digits, int &symbols){
for(int i=0; input[i]!=''; i++){
if(input[i] >='A' && input[i]<='Z'){
upper++;
}
else if(input[i] >='a' && input[i]<='z'){
lower++;
}
else if(input[i] >='0' && input[i]<='9'){
digits++;
}
else{
symbols++;
}
}
}
int main()
{
char s[100];
int lower=0, upper=0, digits=0, symbols=0;
cout << "Enter the string: ";
cin >> s;
count(s, lower, upper, digits, symbols);
cout<<"Lowercase: "<<lower<<", Uppercase: "<<upper<<", Digits: "<<digits<<", Special symbol: "<<symbols<<endl;
return 0;
}
Output:
sh-4.3$ g++ -std=c++11 -o main *.cpp sh-4.3$ main Enter the string: AaBbCc123!@# Lowercase: 3, Uppercase: 3, Digits: 3, Special symbol: 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.