Counting Occurrences Of A Word In A String | C Programming Example

  Рет қаралды 17,257

Portfolio Courses

Portfolio Courses

3 жыл бұрын

An example of counting the occurrences of a word in a string in C Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 53
@jhunecamatura9099
@jhunecamatura9099 Жыл бұрын
Thank you. This is the exact problem we faced during our lab meeting
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Jhune! :-)
@jsnam8139
@jsnam8139 2 жыл бұрын
omg sir you saved my life !! Subscribed immediately! Much thanks!
@theranker3101
@theranker3101 Ай бұрын
Really like your explanations and tutorials. Just finished your C Programming course a couple of days ago and trying out these examples now. I have however, spotted a problem in this code. You're using 'i' to iterate through the string and 'j' to iterate through the word. This approach is great as long as you do not have a word in your string which has the word that you are searching as a part. For example, if the string is "Notwithstanding a brilliant defence, he was found guilty and charged with a second degree offence." If the word being searched for is "with" then the program will display the word count to be 2. Anyways, hope you can find a solution to counter this problem in future videos :)
@MrTrollified
@MrTrollified 2 жыл бұрын
Great explanation
@juju7934
@juju7934 11 ай бұрын
does the function only proceed to the nested for loop when there is a character match?
@PortfolioCourses
@PortfolioCourses 11 ай бұрын
The nested for loop body will run at least once, but if the characters don't match, then it won't run again due to the break statement. :-)
@SketchupGuru
@SketchupGuru Жыл бұрын
I don't understand why you added the Asterix to the function? Why did you make the string a pointer? Isn't it possible to run the function without making it a pointer
@PortfolioCourses
@PortfolioCourses Жыл бұрын
We we pass a string to a function, what we are really passing is a pointer to the first char in the string. So even if we have a function like this: void string_function(char string[]); that is actually the exact same thing as this in practice: void string_function(char *string); It's the same thing with other arrays of different types too when we pass arrays to functions! :-)
@PortfolioCourses
@PortfolioCourses Жыл бұрын
This video may help: kzbin.info/www/bejne/pZaVk42Bn86KqMk. :-)
@MrJzvoyeur
@MrJzvoyeur 5 ай бұрын
one remark here: when a word is found we may do: j += (wlen-1); can the word be found just one position after its 1st occurrence in the string? let the string be "xaaaaaay" and the word be "aa". what result would we expect? 3 or 5?
@mohammedmad235
@mohammedmad235 Жыл бұрын
Here's the corrected version of the sentence: Thank you for this amazing explanation, But we want to know how we can deal with uppercase and lowercase letters and some special characters in a string. I know how to convert lower and upper characters using an ASCII code, but when it comes to special characters like ('), or even spaces as you said before, I have no idea how to work with them.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
What are you trying to do with the special characters Mohammed? What is the problem you are trying to solve? Maybe I can point you in the right direction. :-)
@mohammedmad235
@mohammedmad235 Жыл бұрын
@@PortfolioCourses I would like to handle special characters, spaces, uppercase and lowercase letters in a given string. Specifically, I am interested in identifying occurrences of the word "is," but only when it appears separately and not as part of the sentence "This is me."
@PortfolioCourses
@PortfolioCourses Жыл бұрын
The functions in the ctype library include functions that will identify when a char is uppercase, lowercase, etc, maybe that library can help you: kzbin.info/www/bejne/l4W2e3mlprmNqKs. :-)
@naboulsikhalid7763
@naboulsikhalid7763 7 ай бұрын
hi, good tutorials that make me grow. thanks a lot. I have an issue I used this sentence: "It is is is a different word this time", to count "is", it shows count = 4. is this correct?Can you help
@SamSarwat90
@SamSarwat90 6 ай бұрын
this has one is
@xturki5741
@xturki5741 2 жыл бұрын
i thing in strtok function it will be easier but still Great explanation
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I agree, I might make a video on doing it with strtok() one day too. It’s funny sometimes when students are trying to figure out how to do something they don’t want to see how it’s done with the functions that make it easier because their teachers tell them they can’t use those functions, I guess because they are trying to get them to learn more and so it the hard way. :-)
@xturki5741
@xturki5741 2 жыл бұрын
@@PortfolioCourses I'm a student of software eng. i think the result is the most important thing .. because there are many ways to solve any problem .. also in the real life work we will use the easier and low code lines.. still thank you for everything sir you are awesome .. and if it possible can you make a video how to replace a character with a word in a string
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@xturki5741 I agree, in real-world projects we do it "the easy way". 🙂 This video shows how to replace a substring with another substring: kzbin.info/www/bejne/qnjKnWl7gr-rgrc. So you could have a substring of one character and replace it with a substring made up of a word, and that technique would work.
@xturki5741
@xturki5741 2 жыл бұрын
@@PortfolioCourses Thank you sir you are amazing. big love
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@xturki5741 You're very welcome! 😀
@justcurious1940
@justcurious1940 6 ай бұрын
Amazing,Thanks : // Full code int word_count(char *string, char *word){ int slen = strlen(string); int wlen = strlen(word); int end = slen - wlen + 1; int count = 0; for(int i = 0; i < end; i++){ bool word_found = true; for(int j = 0; j < wlen; j++){ if(word[j] != string[j+i]){ word_found = false; break; } } if(word_found){ ++count; } } return count; }
@sanjayagihan719
@sanjayagihan719 3 жыл бұрын
When the word is "and" & string is "i understand" The output shoud be zero but it will print 1 because of "and" inside the word "understand" how to overcome this error
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
That's a great question! You would need to do some more advanced checking. It would depend on how the string could be formatted, and what assumptions if any you could make. But you could for example only count the occurrence of the word if the character before and the character after the word is not a letter (e.g. a period, a space, etc.). So for example you could call isalpha() with the character before the word and the character after the word, and if it returns true, then you know the "word" you've found is really just part of larger word. If the word is found right at the start of the string then you don't need to check the character before the word (because you can't), and if the word is found at the end of the string then you don't need to check the character after the word (because it's the null terminator). Does that help and make sense? :-) Maybe I can make a video showing how to do this more advanced version too!
@casiebeaton7131
@casiebeaton7131 2 жыл бұрын
@@PortfolioCourses yes i would love to know how to make this check. do you have a video out yet?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@casiebeaton7131 No, but this is still on my list of ideas to get to eventually, so thank you for sharing that you would also be interested in it too! 🙂
@unkown9671
@unkown9671 Жыл бұрын
Same concern hopefully you do a tutorial about it soon
@user-wx1lf5xd2c
@user-wx1lf5xd2c 5 ай бұрын
How to count the occurence of a word without typing the specific word?
@ayzikdig1983
@ayzikdig1983 Жыл бұрын
why the +1 int the int end? is it for the '/o' ?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
If the string is length 10 then we have indexes: string[0] = string[1] = string[2] = string[3] = string[4] = string[5] = w1 string[6] = w2 string[7] = w3 string[8] = w4 string[9] = w5 string[10] = '\0'; So then if the word is length 5, the last possible index the word could begin is index 5. Therefore, if we reach 10 - 5 + 1 = 6 then we can stop looking at that point. :-) It's just "the index where we can stop looking for the word is the index 1 beyond (+1) the last index where the word could still fit in the string".
@ayzikdig1983
@ayzikdig1983 Жыл бұрын
@@PortfolioCourses thanks for the reply
@__iTsMe__
@__iTsMe__ Жыл бұрын
before i watch the video i came with this solution: int word_count(char *str, const char *word) { int word_len = strlen(word), count = 0, i = 0, j = 0; while (str[i] != '\0') { if ( {tolower(str[i]) - tolower(word[j]) == 0) { j++; if (j == word_len) { count++; j = 0; } } i++; } return count; } is an efficient solution or not...? can i have feedback on this function..?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I don't think that function will produce the correct result. What if the beginning of the word is found somewhere in the string, but not the entire word? In that case j is never 'reset' to 0 by this code.
@__iTsMe__
@__iTsMe__ Жыл бұрын
@@PortfolioCourses Thank You for time
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@__iTsMe__ You're welcome! 🙂
@nishka9737
@nishka9737 2 жыл бұрын
Can you please do this program using StringTokenizer?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I don't think StringTokenizer is a thing that's available in C, but I could be wrong. Do you have a link to the library? Or are you asking me to make a video on using StringTokenizer in Java or C++ or another language?
@nishka9737
@nishka9737 2 жыл бұрын
@@PortfolioCourses yeah im asking you to do this in java language. java.util.StringTokenizer is there.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Ok, one day I am hoping to do Java videos but it may not be for awhile yet.
@nishka9737
@nishka9737 2 жыл бұрын
@@PortfolioCourses oh okay. Thanks.
@deepblackoutlaw9640
@deepblackoutlaw9640 5 ай бұрын
the way i solved it before watching the video: int word_count(const char* str, const char* word) { size_t str_length = strlen(str); size_t word_length = strlen(word); int counter = 0; for (size_t i = 0; i < str_length; i++) { if (*(str + i) == *(word + 0)) { size_t j = 1; for (j; j < word_length; j++) { if (*(str + (i + j)) != *(word + j)) { break; } } i += j; if (j == word_length) { counter++; } } } return counter; }
@aminetakha
@aminetakha Жыл бұрын
Hello sir, I come up with the following solution. Do you think it is efficient #include #include #include int main() { char string[] = "world worlhdworld"; char word[] = "world"; char *sub = malloc(sizeof(word)); int count = 0; for (int i = 0; i < strlen(string); i++) { if (string[i] == word[0]) { strncpy(sub, string + i, strlen(word)); int is_equal = strcmp(sub, word); if (is_equal == 1) { count++; i += strlen(word) - 1; } } } free(sub); return 0; }
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question Amine! I think it would not be too inefficient. :-) The strcmp() is not much different from just checking for a match "in-place" in the existing string. The only thing is that it's doing a strcpy() for each possible occurrence of the word based on matching the first letter, and that part could be expensive because it copies the whole word each time. Especially if it's a common first letter, like how 'e' is commonly occurring in English. The other thing is that sub has sizeof(word) but I think you need sizeof(word) + 1 for the null terminator too? Overall, this wouldn't be terribly inefficient or anything, but it's going to be more efficient to just check for the match "in place" rather than do a copy.
@aminetakha
@aminetakha Жыл бұрын
@@PortfolioCourses Thank you for your response sir. Aah I see, it not going to be good if the first character occurs multiple times plus I forgot the '\0'. Yes your solution is better, I tried to do it with one single for loop but it seems that using an inner loop is probably better
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome. And yes, internally strcmp() does a loop, so calling strmcp() inside a loop is already like having a loop inside a loop, in terms of efficiency. :-)
@MrJzvoyeur
@MrJzvoyeur 5 ай бұрын
one remark (or question) here: when a word is found we may do: if (word_found) { count++; i += (wlen-1); } which means: a word of more than one character should not be found just one position after its 1st character in the string. let the string be "xaaaaaay" and the word be "aa" . the result of counting should be: 3, not 5, right?
@PortfolioCourses
@PortfolioCourses 5 ай бұрын
It depends on how you want to do it really, you could do it either way and still call it a solution to this general problem.
@MrJzvoyeur
@MrJzvoyeur 5 ай бұрын
@@PortfolioCourses Yes, i aggree,, it means listening carefully to the task
@Abdalrahman_0
@Abdalrahman_0 Жыл бұрын
--> This is my solution before watching your video ------------------ #include #include int is_found(char string[], char word[]){ int count=0, i; char W[20]; for(i=0; string[i]; i+=(strlen(W)+1) ){ sscanf(string+i, "%[^' ']", W); if( !strcmp(word, W) ) count++; } return count; } int main(void) { char string[]= "Hi Hello Society Hi Hi Hi Hello Hello Society"; char word[10]; printf("What's the word that u search for: "); scanf("%s", word); printf(is_found(string, word)? "Found %d times " : "Not found ", is_found(string, word)); return 0; }
Count The Words In A String | C Programming Example
8:08
Portfolio Courses
Рет қаралды 12 М.
Infinite Input Buffer | C Programming Example
13:08
Portfolio Courses
Рет қаралды 15 М.
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 39 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 17 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
Counting the Vowels in a String | C Programming Example
9:50
Portfolio Courses
Рет қаралды 15 М.
C_69 C Program to Reverse a String | with strrev() and without strrev() function
24:51
Count The Occurrences Of A Character In A String | C++ Example
6:41
Portfolio Courses
Рет қаралды 15 М.
Check If A String Is A Palindrome | C Programming Example
10:56
Portfolio Courses
Рет қаралды 36 М.
How C++ Works
20:21
The Cherno
Рет қаралды 1 МЛН
This is the best way to learn C++ for free
0:40
Mehul - Codedamn
Рет қаралды 399 М.
ОПАСНАЯ ВЕЛОДОРОЖКА СТОЙ ⛔️
0:18
Леха МАК
Рет қаралды 1,8 МЛН
¡Toro cerril en Useras! (¡Espectacular!) #toros #useres #bull
0:19
Multi Toros
Рет қаралды 21 МЛН
ОПАСНАЯ ВЕЛОДОРОЖКА СТОЙ ⛔️
0:18
Леха МАК
Рет қаралды 1,8 МЛН
Света квадробер в парке! Часть 4 #shorts
0:31
Настя AmyMyr
Рет қаралды 1,6 МЛН
😳 Это ПРЕВРАЩАЕТ всё в ПИЦЦУ !
0:28
Настя, это где?
Рет қаралды 8 МЛН
Cute❤️‍🔥😍
0:10
H Hi
Рет қаралды 13 МЛН