• Skip to main content
  • Skip to footer

SoftwareProg.com

Software programming learning aide

  • Blog
    • Discussion
  • News
  • CS50’s Introduction to Computer Science by Harvard University on Edx
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5

Char type while counting number of words in Readability project

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Char type while counting number of words in Readability project

Tagged: char type

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • December 25, 2021 at 9:07 am #124
    admin
    Keymaster


      Copy Code
      Copied
      Use a different Browser

      
      
      #include 
      #include 
      #include 
      int main(void)
      {
          string enterword = get_string("input: ");
          int t = strlen(enterword);
          printf("%i\n", t);
          int counter = 0;
          for(int i = 0; i<= t; i++)
          {
         
               if (enterword == '.'|| enterword == '!' || enterword == '?'){
               counter = counter + 1;}
          }
               printf("number of lines %i\n",counter);
           
               int wordcounter = 0;
               for (int i = 0; i<= t; i++)
               {
                   if (enterword == ' ' || enterword == '.'){
                   wordcounter = wordcounter + 1;
               }
               
               }
               printf("total words %i\n", wordcounter);
       }

      The above program fails to calculate correctly no. of words. The problem is that char type can take only one character.


      Copy Code
      Copied
      Use a different Browser

      
      
      int wordcounter = 0;
      for (int i = 0; i<= t; i++)
           {
               if (enterword == ' ' || enterword == '.'){
               wordcounter = wordcounter + 1;
               }
            }
           printf("total words %i\n", wordcounter);

       

      I cannot have enterword == ‘. ‘; that is, one space after. to count correctly number of words when before full stop and a new line starts with a space after that full stop. Seeking help how to approach in such scenario.


      Reply


      https://cs50.stackexchange.com/questions/42261/char-type-while-counting-number-of-words-in-readability-project

      https://edstem.org/us/courses/176/discussion/976408?comment=2210979[learn_press_profile]

    • Author
      Posts
    Log In Register Lost Password
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Log in / Register

    Initiative by Digital Splendid OPC Pvt. Ltd.