• Skip to main content
  • Skip to footer

CS50's Introduction to Computer Science on Edx: Supplementary resource

CS50 threads to aide as a supplementary resource

Readability lab: Counting number of words and sentences after counting number of alphabetical characters

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Readability lab: Counting number of words and sentences after counting number of alphabetical characters

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • October 2, 2022 at 8:41 am #709
    admin
    Keymaster


      Copy Code
      Copied
      Use a different Browser

      
      
      #include <stdio.h>
      #include <cs50.h>
      #include <string.h>
      #include <ctype.h>
      
      int lettercounter(string enteredword);
      int wordcounter(string enteredword);
      
      int main(void) {
          string enteredword = get_string("enter word:");
          int y = lettercounter(enteredword);
          printf("number of alphabetical characters: %i\n", y);
      
          int f = wordcounter(enteredword);
          printf("number of words: %i\n", f);
      }
      
      int lettercounter(string enteredword) {
          int t = strlen(enteredword);
          int x = 0;
          int c = 0;
          for (x = 0; x < t; x++) {
              if (isalpha(enteredword[x])) {
                  c = c + 1;
              }
          }
          return c;
      }
      
      int wordcounter(string enteredword) {
          int t = 0;
          int c = 1;
          int x = strlen(enteredword);
          for (t = 0; t < x; t++) {
              if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?') {
                  c = c + 1;
              }
      
          }
          return c;
      
      }

      readability lab

      Counting number of sentences is almost similar to counting number of words with only one difference:


      Copy Code
      Copied
      Use a different Browser

      
      
      if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')

      Instead of:


      Copy Code
      Copied
      Use a different Browser

      
      
       if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')

       


      Copy Code
      Copied
      Use a different Browser

      
      
      #include <stdio.h>
      #include <cs50.h>
      #include <ctype.h>
      #include <string.h>
      int lettercounter(string enteredword);
      int wordcounter(string enteredword);
      int sentencecounter(string enteredword);
      int main(void)
      {
          string enteredword = get_string("enter word:"); int y = lettercounter(enteredword);
          printf("number of alphabetical characters: %i\n", y);
          int f = wordcounter(enteredword);
          printf("number of words: %i\n", f);
          int s = sentencecounter(enteredword);
          printf("number of sentences: %i\n",s);
      }
      
      
      int lettercounter(string enteredword)
      {
      int t = strlen(enteredword);
      int x = 0;
      int c = 0;
          for (x = 0; x < t; x++)
          {
              if (isalpha(enteredword[x]))
              {
              c = c + 1;
              }
      
          }
      return c;
      }
      
      
      int wordcounter(string enteredword)
      {
      int t = 0;
      int c = 1;
      int x = strlen(enteredword);
          for (t = 0; t < x; t++)
          {
              if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')
              {
              c = c + 1;
              }
      
          }
      return c;
      }
      
      
      
      int sentencecounter(string enteredword)
      {
      int s = 0;
      int x = strlen(enteredword);
      int c = 0;
          for (s = 0; s < x; s++)
          {
              if(enteredword == '.' || enteredword == '!'  || enteredword == '?')
              {
              c = c + 1;
              }
          }
      return c;
      }

      cs50

       [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.