Word Count

Hi, I have to create a simple program that can compute the number of words, bytes, and lines that are represented in a string. I have a bit of a problem because I can compute the bytes with no problem but I'm having a bit of a hard time computing the lines and the number of words in the string. Any help would be gladly appreciated!

#include<iostream>
using namespace std;

int main( )
{
char ch[30];
int line=1, words=0;
string bytes;

getline(cin,bytes);
cin.getline(ch,30);


for(int i = 0; ch [i] != '\0'; i++){
if ( ch [i] == ' '){
words++;
}
else if (ch [i] == '\n'){
line++;
}
}
cout<< '\n' << line << '\t' << words+1 << '\t' << bytes.size()<< endl;

return 0;
}
Topic archived. No new replies allowed.