dmilicev

count letters and words in string v1.c

Oct 13th, 2019
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. /*
  2.     count letters and words in string v1.c
  3. */
  4.  
  5. #include<stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     int i = 0;                      // loops counter
  10.     int countLetters = 0;           // counter for letters
  11.     int countWords = 0;             // counter for words
  12.  
  13.     char str[] = "count how many words and letters there are";  // string to be analyzed
  14.  
  15.     while (str[i] != '\0')          // from beginning to end of string
  16.     {
  17.         if ( str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )
  18.         {                           // if str[i] is letter, this is a new word and
  19.             countWords++;           // increment countWords
  20.  
  21.             while(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )
  22.             {                       // then, while str[i] is letter
  23.                 countLetters++;     // increment countLetters
  24.                 i++;                // go to next character
  25.             }
  26.         }
  27.         else                        // if str[i] is not letter
  28.             i++;                    // go to next character
  29.     }
  30.  
  31.     printf("\n %s \n",str);
  32.  
  33.     printf("\n countWords = %d \n",countWords);
  34.  
  35.     printf("\n countLetters = %d \n",countLetters);
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment