• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #701
    admin
    Keymaster

      C function that calculates length of alphabetical characters operating on string?

      Strlen function calculates the length of a string. Isalpha instead of operating on a string I understand operates on character, differentiating alphabetical characters from other characters. So while one can count length of string by one line of code:

      
      
      string enteredword = get_string("enter word");
      
      int x = strlen(enteredword);

       

      The same cannot be with isalpha.

      So one cannot have this way:

      
      
      string enteredword = get_string("enter word");
      
      int x = isalpha(enteredword);

      Is there known function that performs the operation of counting alphabetical characters?

      Nevertheless, the purpose of this course is coding and resorting to function in this case will not help achieve the purpose.


      Reply


      https://edstem.org/us/courses/176/discussion/1870379?answer=4273443


      Here is the code that counts the number of alphabetical characters:

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

      readability lab

       

       [learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top