0

My for loop doesn't seem to be executing. When i run the program it seems for loop is been skipped. Can someone please help me out.

#include <stdio.h> #include <cs50.h> #include <string.h> #include <math.h>

int main (void) { int letters=0, words=0, sentences=0; char text[100]; printf("Enter text:"); scanf("%s",&text[100]); //To prompt the user to type in text

for(int i=0 ; i<strlen(text); i++)    //Loop to count letters words and sentences
{
    if ((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z'))
   {
       letters++;
   }
   
   else if (text[i] == ' ')
   {
       words++;
   }
   else if (text[i] == '.' || text[i] == '!' || text[i] == '?')
   {
       sentences++;
   }
}
printf("Letters: %i\nWords: %i\nSentences: %i\n", letters, words, sentences);

}

1 Answer 1

0

To see the whole picture, add text to the printf.

From man scanf re the %s directive:

s

Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.

Why not take advantage of the cs50 functions?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .