CS50x threads to aide as a supplementary resource › 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.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
October 2, 2022 at 8:41 am #709
#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; }
Counting number of sentences is almost similar to counting number of words with only one difference:
if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')
Instead of:
if (enteredword[t] == ' ' || enteredword[t] == '!' || enteredword[t] == '?')
#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; }[learn_press_profile]
-
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.