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 letters
Tagged: counter, string length, string type, string.h, strlen
- This topic is empty.
-
AuthorPosts
-
December 29, 2021 at 9:10 am #126
[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> 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 = 1; for (int i = 0; i <= t; i++) { if (enterword[i] == ' ') { wordcounter = wordcounter + 1; } } printf("total words %i\n", wordcounter); }
[/dm_code_snippet]
After getting counted the number of words, sentences, it is time to count number of letters. Here is my tentative idea:
lettercounter = 0
Total characters are known by string length function.
lettercounter will be lettercounter = lettercounter + 1 whenever there are alphabets till it reaches string length. Now, one point to clear is how alphabets will be distinguished from digits, punctuation marks. Is my approach correct?
Reply
https://cs50.stackexchange.com/questions/42285/readability-lab-counting-letters[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.