counting punctuation in a phrase???
I wrote a code how to count uppercase letter, lowercase letters, numbers, spaces, but cant figure out punctuation...any ideas...
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int upper = 0,
lower = 0,
digit = 0,
spaces = 0;
char ch [80];
int i;
cout << "Enter a phrase: \n";
gets(ch);
for(i = 0; ch[i]!='\0'; i++)
{
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
else if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
else if (ch[i] >= '0' && ch[i] <='9')
digit++;
else if (ch[i] >= ' ' && ch[i] <= ' ')
spaces++;
}
cout << "Number of Upper case letters: " << setw(5) << upper << endl;
cout << "Number of lower case letters: " << setw(5) << lower << endl;
cout << "Number of digits: " << setw(17) << digit << endl;
cout << "Number os spaces: " << setw(17) << spaces << endl;
return 0;
}
|
Hi,
Use ispunct() :
1 2
|
#include <cctype>
int punct = 0;
|
And :
1 2 3
|
else if (ch[i] >= ' ' && ch[i] <= ' ')
spaces++;
else if(ispunct(ch[i]) punct++;
|
And :
cout << "Number of punctuations : " << setw(17) << punct << endl;
Does that help you? :)
Yeah that works too. I ended up using : else if (ch[i] != ' ')
special++;
> Yeah that works too.
Does that mean your problem is solved now? :)
Topic archived. No new replies allowed.