how to count spaces in a line

wads the problem with tis...

int digit=0,consonant=0,space=0,vowel=0,i,c;
char text[256];

cout<<"Input a line of text terminated with the @ character -> ";
cout<<endl;
cin.getline(text,256,'\n');

for (i=0;i<=256;i++)
{
c=text[i];
if (isdigit(c))
{
digit++;
}
}
Your for loop walks beyond the end of the string.

EDIT: not only beyond the end of the string the user typed, which can be less than 255 characters, but also beyond the end of the array.
Last edited on
That program is counting the number characters in the string. If you want to make it counting the spaces in it, you should just change the if condition
Topic archived. No new replies allowed.