Help with this finding the number of occurences of a character program
Feb 23, 2014 at 1:57am UTC
Okay, it works great and I am so close in solving this out. The only problem is that, it outputs the number value of the ascii character instead of outputting the letters.
Here's the output:
https://www.dropbox.com/s/3h205080grg8rtl/result.png
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch;
char text[30], f=0;
int i, n, alpha;
cout << "Enter a text: " ;
cin.getline(text, 30);
strlwr(text);
for (alpha = 97; alpha <= 122; alpha++)
{
n = 0;
f = 0;
for (i=0; text[i] != NULL; i++)
{
if (alpha == text[i])
{
n++;
f = 1;
}
}
if (f == 1)
{
cout << alpha << " value found " << n << " times\n" ;
}
}
system ("pause" );
return 0;
}
Feb 23, 2014 at 2:45am UTC
Try just casting 'alpha' into a 'char' value rather than an int. Alternately, just have alpha be a char to start with!
Feb 23, 2014 at 1:32pm UTC
Why didn't I noticed that? But thanks, it's now working!
Topic archived. No new replies allowed.