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 number of words and sentences after counting number of alphabetical characters
- This topic is empty.
-
AuthorPosts
-
October 2, 2022 at 8:41 am #709
[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 <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; }
[/dm_code_snippet]
Counting number of sentences is almost similar to counting number of words with only one difference:
[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”]
if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')
[/dm_code_snippet]
Instead of:
[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”]
if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')
[/dm_code_snippet]
[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 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[s] == '.' || enteredword[s] == '!' || enteredword[s] == '?') { c = c + 1; } } return c; }
[/dm_code_snippet]
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.