Objectives: using functions C-Strings. Write a C++ program that Asks the user for a string input (c-string to be precise).
QUESTION
Objectives: using <cctype> functions C-Strings.
Write a C++ program that
(1) Asks the user for a string input (c-string to be precise). Input can have white spaces. So, make sure you use the cin.getline() function instead of the cin object.
(2) The string can contain numbers, punctuation, spaces, tabs and any other. You have to find the following:
Find the number of words – words are separated by a space or a tab or a punctuation follows a word.
Find the number of numeric characters (0-9) – use the isdigit() function
Find the number of spaces or tabs – use the isspace() function
Find the number of punctuations – use the ispunct() function
Find the number of consonants – use the isalpha() function, you can use tolower() or toupper() functions to make comparisons case insensitive.
You can convert the string into lower case before calling the functions or making the comparisons.
Display the above mention statistics.
SUMMARY
A random string that consists of white spaces is taken in as output from the user. The number of whitespaces, number of digits, punctuations, consonants, and number of words in the string is calculated and printed as the output to the console.
EXPLANATION
The string is taken in as input is declared using the getline() method to get the white spaces separately. To count the number of words, a variable called ‘w’ is declared. Similarly, for the number of digits, punctuations, and consonants, variables ‘d’, ‘cons’, and ‘punc’ is declared and initialized to zero. The string is then traversed and each character present is checked if it is a space, digit, or consonant. Based on what the character is, that corresponding variable is incremented by one. Finally, the number of words, digits, consonants, punctuations, and spaces are printed as output to the console.
CODE
#include <iostream> using namespace std; int main() { string st; cout<<"Enter the string: "<<endl; /* to get the string from user with spaces */ getline(cin,st); cin.ignore(); /* the number of words */ int w=1; /* to count the number of digits in the string */ int d=0; /* to have number of consonants */ int cons=0; /* to count the number of punctuations */ int punc=0; /* to traverse the string */ for(int i=0;i<st.length();i++) { /* to check if the current character is space or not */ if(isspace(st[i])) w++; /* to check if the current character is digits or not */ else if(isdigit(st[i])) d++; /* to check if the current character is punctuation or not */ else if(ispunct(st[i])) punc++; /* to check if the current charatcer is alphabet or not */ if(isalpha(st[i])){ /* to check if the current character is vowel or not */ char p=tolower(st[i]); if(p=='a'||p=='e'||p=='i'||p=='o'||p=='u') {} else cons++; } } /* printing the desired result */ cout<<"\nNumber of words: "<<w<<endl<<endl; cout<<"Number of digits: "<<d<<endl<<endl; cout<<"Number of spaces or tabs: "<<w-1<<endl<<endl; cout<<"Number of punctuations: "<<punc<<endl<<endl; cout<<"Number of consonants: "<<cons<<endl<<endl; }
OUTPUT
Also Read: Write a python code that fulfills the following requierments