2. Please help! :) Simple C++ Programming. Please make sure it compiles. Write a
ID: 3581825 • Letter: 2
Question
2. Please help! :) Simple C++ Programming. Please make sure it compiles.
Write a program to ask the user to enter a string. The string can be of any length and contain any ASCII characters (digits, alphabets, special characters, etc.). Write a void return type function to count the number of digits, the number of alphabets and the number of special characters in the string the user has entered. You need to pass to the function the string the user has entered and the function needs to display the counts on the screen. Choose either C-string or C++ string library.Explanation / Answer
#include<iostream>
#include<cstring>
void strcount(char[]);
int main()
{
char str[100];
int len;
std::cout<<"Enter the string"<<" ";
std::cin>>str;
strcount(str);
}
void strcount(char str[])
{
int i,len;
int dig=0,alpha=0,sp=0;
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9')
dig++;
else if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
alpha++;
else
sp++;
}
std::cout<<"number of digits"<<dig<<" ";
std::cout<<"number of alphabets"<<alpha<<" ";
std::cout<<"number of special characters"<<sp<<" ";
}
sample output:
Enter the string hi!buddy007##$%
number of digits3
number of alphabets7
number of special characters5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.