Count The Words In A String | C Programming Example

  Рет қаралды 12,483

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 25
@SketchupGuru
@SketchupGuru Жыл бұрын
Getting back to learning after a long break!! I'm a slow learner but learning with each video
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Awesome to hear you're back to learning! :-)
@Tad4546
@Tad4546 2 жыл бұрын
Damn, stuff's well explained, thanks :)
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@ihfazhakimsulvi6494
@ihfazhakimsulvi6494 Ай бұрын
what if we like put a condition that we will treat those as words only starting with alphabets?
@justcurious1940
@justcurious1940 5 ай бұрын
Interesting method.
@BlackCat-fx9kb
@BlackCat-fx9kb 2 жыл бұрын
Do you have a video covering how to save a full sentence input and then separate each word into an array?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Not exactly. But this video might help you with that, you could split the words in the sentence based on 'space': kzbin.info/www/bejne/jKGZgHiCgJmao9E.
@petepudrick
@petepudrick 2 жыл бұрын
Thank you, it helps a lot !
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@animeproz6780
@animeproz6780 2 жыл бұрын
How if there is a nonwords at the beginning of the string?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
We could use an extra loop before the for-loop to skip through this nonword content, like this code below. Maybe this version will become a new video for handling this case. :-) #include #include int word_count(char *s); int main() { char s[] = " This is, the way. Another..."; int count = word_count(s); printf("%d ", count); return 0; } int word_count(char *s) { int len = strlen(s); int count = 0; char nonwords[] = " .,; \t"; // skip though a possible nonword at the start of the string int i = 0; while (i < len) { if (strchr(nonwords, s[i]) == NULL) break; i++; } for ( ; i < len; i++) { while (i < len) { if (strchr(nonwords, s[i]) != NULL) break; i++; } count++; while (i < len) { if (strchr(nonwords, s[i]) == NULL) break; i++; } } return count; }
@duytran410
@duytran410 2 жыл бұрын
Thank you sir . Your content is gold. I watch all your video and practice. Thanks again ^_^ #include #include #include int main(){ char str[] = " ;.Welcome ,. to c 2022"; int letter = 0; int word = 0; int digit = 0; int length = strlen(str); char not_letters[] = " ,;."; for(int i = 0 ; i < length; i++) { if(isalpha(str[i])){ while(i < length) { if(strchr(not_letters, str[i]) != NULL) break; // increase loop i++; // count letter letter++; } word++; } if(isdigit(str[i])) { digit++; } } printf("Letter: %d ", letter); printf("Word: %d ", word); printf("Digit: %d ", digit); return 0; }
@jor1994
@jor1994 Жыл бұрын
What if I am trying to count the words from a string already obtained from the user via get_string? I am getting confused with what the "char s[] = ?" would equal in this scenario. Video was super helpful at explaining the why behind all your lines, thank you!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question Jordan! :-) The get_string() function appears to be from the CS50 course, so the function isn't really part of the C programming language officially. I would recommend using the standard C way of dealing with strings, which is to store them into a char array. This video may help you with accepting string input from the user in other ways: kzbin.info/www/bejne/nGmYaWyPbrF7fsk.
@jor1994
@jor1994 Жыл бұрын
@@PortfolioCourses Thank you so much for replying, you are correct on the cs50 course. Coming from python and taking this course to get a better understanding of CS in general, your videos have really helped connect some dots.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's excellent hear that Jordan! :-)
@gnesha5904
@gnesha5904 Жыл бұрын
I’m having a strange problem where my word count function is counting trailing white spaces as a word For example using strtok I pass a string that would say “hello my name is Nice to meet you” (assume the is an actually enter key on a .txt file and the trailing spaces r at the end of the line) my word count would return 5 when there’s only 4 words. Any help?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I don't know, people would likely need to see your code to be able to help you with that problem. :-)
@Mr_wisk
@Mr_wisk 2 жыл бұрын
does this work with non predefined words? like if i make the user input a text?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Yes, it will work with non predefined words. :-)
@Mr_wisk
@Mr_wisk 2 жыл бұрын
@@PortfolioCourses ty i got it to work after some adjusting this helped a lot
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@Mr_wisk Awesome, I'm glad to hear that! 😀
@mehedihasan_5267
@mehedihasan_5267 Жыл бұрын
#include int main(){ int i=0,word=0; char text[100]=" ok sfs rfd yes its working.Apple ?? " ; while(text[i]!='\0'){ switch(text[i]){ case ' ': if(i==0){ i++;} else word++; while(text[i]='A'){ word++;} i++; break; } i++; } printf("%d",word); }
@PortfolioCourses
@PortfolioCourses Жыл бұрын
This looks like it should be able to solve the problem OK Mehedi. :-)
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 63 МЛН
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 45 МЛН
Counting the Vowels in a String | C Programming Example
9:50
Portfolio Courses
Рет қаралды 15 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
do you know how "return" works under the hood? (are you SURE?)
5:08
Low Level Learning
Рет қаралды 354 М.
Introduction To Threads (pthreads) | C Programming Tutorial
13:39
Portfolio Courses
Рет қаралды 93 М.
How computer processors run conditions and loops
17:03
Core Dumped
Рет қаралды 73 М.
How to split strings in C (strtok)
9:28
CodeVault
Рет қаралды 106 М.
Men Vs Women Survive The Wilderness For $500,000
31:48
MrBeast
Рет қаралды 63 МЛН