IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Readability lab: Counting alphabetical characters with isalpha function in ctype.h header file
Tagged: isalpha, isalpha function, readability, readability problem
- This topic is empty.
-
AuthorPosts
-
October 2, 2022 at 7:16 am #701
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:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
string enteredword = get_string("enter word"); int x = strlen(enteredword);
[/dm_code_snippet]
The same cannot be with isalpha.
So one cannot have this way:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
string enteredword = get_string("enter word"); int x = isalpha(enteredword);
[/dm_code_snippet]
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:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#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; }
[/dm_code_snippet]
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.