Hello,
I have looked at some of the other threads that are similar to my dilemma, but they don't seem to capture what I'm experiencing. I basically have to make a program (specifically using the string class versus C-string) that allows a user to type a line of text. It then has to figure out how many words there are in the line of text and give a count of letters (making sure that lower case and upper case are seen as the same letter). The following code doesn't include my part for the word counting, as I have that under control. The part I can't quite get is the letter count. Here's my code thus far:
Code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main ()
{
int letter_count;
int words=0;
int counter[26]={0};
string line;
cout << "Enter a line of text:\n";
getline(cin,line);
for (letter_count=0; letter_count <=line.length(); letter_count++)
{
if (isalpha(line[letter_count]))
{
line[letter_count]=tolower(line[letter_count]);
cout << counter[line[letter_count]] << endl ;
counter [line[letter_count] - 'a']++;
} else
{
words=words+1;
}
return 0;
}
Sample output:
Enter a line of text:
I say hi.
i 1
s 44
a 2
y 0
h 112
i 1
I assuming that instead of giving counts for each of these letters, it is displaying the ASCII representation instead. I am not sure how to get to do the following instead:
Enter a line of text:
I say hi.
i 2
s 1
a 1
y 1
h 1
Any suggestions/guidance that could be provided on this would very very much appreciated.
Thank you!