This program should be pretty easy to write if you can use the <cctype> header file.
Are you allowed to use that header file?
If so, it has all the necessary functions needed to check for words,digits,symbols.
Take a look here:
http://www.cplusplus.com/reference/cctype/
Then you can just make if statements to check for certain scenarios.
Like
if(isdigit)
digitcounter++;
Secondly you're going to use for loops for everything.
Because you want to look at each character in a string.
That reminds me, what is your input going to be?
Are you reading in words per line in a file or what?
If for example the input was something like this:
This sentence has spaces and the numbers 123 with @# symbols. |
Do you want to treat the entire line as one big string or take each element separated by white space as individual strings?
If the whole sentence was 1 giant string then use the getline function to read in the entire line.
So if I wanted to count the number of digits in that string, I would do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void check_digits(string)
int main()
{
string phrase;
cout << "Please enter a phrase " << endl;
cin >> phrase;
check_digits(phrase);
return 0;
}
void check_digits(string word)
{
int digcount = 0;
n = word.length(); // finds length of word;
for (int i = 0; i < n; i++)
{
if(isdigit(word[i]))
digcount++;
}
cout << "There are " << digcount << "digit(s) in "<< word << endl;
}
|