Outline ·
[ Standard ] ·
Linear+
How do count letters in C?
TSHumbleBF
|
May 10 2020, 06:49 PM, updated 4y ago
|
|
Real beginner in C here.
Let's say for example I want to count the letters in the sentence.
Like this sentence.
There is 15 letters above^
show 15 letters
#include <stdio.h> #include <cs50.h> #include <string.h> #include <ctype.h>
int main(void) { //prompt user input string t = get_string("Text: "); //count no.of letter(s) printf("no.of character letter(s)"); } printf("\n");
What function could i use to count letters?
|
|
|
|
TSHumbleBF
|
May 10 2020, 06:49 PM
|
|
Sorry theres 16 letters
|
|
|
|
WongGei
|
May 10 2020, 06:57 PM
|
|
count() in algorithm
This post has been edited by WongGei: May 10 2020, 06:57 PM
|
|
|
|
malleus
|
May 10 2020, 07:51 PM
|
|
try to imagine, explaining to a 5 year old how to count the number of letters. how would you do that?
|
|
|
|
mmweric
|
May 10 2020, 09:11 PM
|
Getting Started
|
QUOTE(HumbleBF @ May 10 2020, 06:49 PM) Real beginner in C here. Let's say for example I want to count the letters in the sentence. Like this sentence. There is 15 letters above^ show 15 letters #include <stdio.h> #include <cs50.h> #include <string.h> #include <ctype.h> int main(void) { //prompt user input string t = get_string("Text: "); //count no.of letter(s) printf("no.of character letter(s)"); } printf("\n"); What function could i use to count letters? Do a Google search or check the reference manual faster then posting a question. First thing you need to learn in programming is how to read the reference manual or learn how to use Google . This post has been edited by mmweric: May 10 2020, 09:14 PM
|
|
|
|
TheSolver
|
May 13 2020, 09:43 PM
|
|
Congratulations you're learning the most popular programming language this month of May according to Tiobe Index . Basically, you're trying to implement like this in Python: CODE s = raw_input("Text: ") print("no.of character letter(s): " + str(len(s))) print("show letters: " + s);
The C Language (C99) equivalence is: » Click to show Spoiler - click again to hide... « CODE #include <stdio.h>
#define BUFLEN 1024
char *get_string(const char *prompt, char *s) { printf("%s", prompt); scanf("%[]a-zA-Z0-9_%'`.,;:!@#$^&*()=+{}?<>|/ \\\"[-]", s); return s; }
int main(void) { char s[BUFLEN], *cp;
cp = get_string("Text: ", s); //prompt user input //scanf("%s", s); //position end pointer to calculate and print no.of letter(s) while (*cp++) ; printf("no.of character letter(s): %ld\n", cp - s - 1L);
printf("show letters: %s\n", s); return 0; }
The function you're interested is strlen() from <string.h> but strlen() itself is written using pointers anyway, so the routine is explicitly written for you. C is elegant isn't it? I know you're a beginner, but C is hard as demonstrated in the spoiler above. You might want to stick to Python instead.
|
|
|
|
TSHumbleBF
|
May 13 2020, 10:48 PM
|
|
QUOTE(TheSolver @ May 13 2020, 09:43 PM) Congratulations you're learning the most popular programming language this month of May according to Tiobe Index . Basically, you're trying to implement like this in Python: CODE s = raw_input("Text: ") print("no.of character letter(s): " + str(len(s))) print("show letters: " + s);
The C Language (C99) equivalence is: » Click to show Spoiler - click again to hide... « CODE #include <stdio.h>
#define BUFLEN 1024
char *get_string(const char *prompt, char *s) { printf("%s", prompt); scanf("%[]a-zA-Z0-9_%'`.,;:!@#$^&*()=+{}?<>|/ \\\"[-]", s); return s; }
int main(void) { char s[BUFLEN], *cp;
cp = get_string("Text: ", s); //prompt user input //scanf("%s", s); //position end pointer to calculate and print no.of letter(s) while (*cp++) ; printf("no.of character letter(s): %ld\n", cp - s - 1L);
printf("show letters: %s\n", s); return 0; }
The function you're interested is strlen() from <string.h> but strlen() itself is written using pointers anyway, so the routine is explicitly written for you. C is elegant isn't it? I know you're a beginner, but C is hard as demonstrated in the spoiler above. You might want to stick to Python instead. In fact... C is my first programming language, I have no basic in programming...and now I'm amazed at how simplified Python is ...
|
|
|
|