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 › Char type while counting number of words in Readability project
Tagged: char type
- This topic is empty.
-
AuthorPosts
-
December 25, 2021 at 9:07 am #124
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include #include #include int main(void) { string enterword = get_string("input: "); int t = strlen(enterword); printf("%i\n", t); int counter = 0; for(int i = 0; i<= t; i++) { if (enterword[i] == '.'|| enterword[i] == '!' || enterword[i] == '?'){ counter = counter + 1;} } printf("number of lines %i\n",counter); int wordcounter = 0; for (int i = 0; i<= t; i++) { if (enterword[i] == ' ' || enterword[i] == '.'){ wordcounter = wordcounter + 1; } } printf("total words %i\n", wordcounter); }
[/dm_code_snippet]
The above program fails to calculate correctly no. of words. The problem is that char type can take only one character.
[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”]
int wordcounter = 0; for (int i = 0; i<= t; i++) { if (enterword[i] == ' ' || enterword[i] == '.'){ wordcounter = wordcounter + 1; } } printf("total words %i\n", wordcounter);
[/dm_code_snippet]
I cannot have enterword[i] == ‘. ‘; that is, one space after. to count correctly number of words when before full stop and a new line starts with a space after that full stop. Seeking help how to approach in such scenario.
Reply
https://edstem.org/us/courses/176/discussion/976408?comment=2210979[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.