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

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thank you. This is the exact problem we faced during our lab meeting

jhunecamatura
Автор

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 :)

theranker
Автор

omg sir you saved my life !! Subscribed immediately! Much thanks!

jsnam
Автор

does the function only proceed to the nested for loop when there is a character match?

juju
Автор

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 and the word be "aa".
what result would we expect? 3 or 5?

MrJzvoyeur
Автор

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

naboulsikhalid
Автор

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

SketchupGuru
Автор

why the +1 int the
int end? is it for the '/o' ?

ayzikdig
Автор

How to count the occurence of a word without typing the specific word?

wxlfxdc
Автор

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.

mohammedmad
Автор

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..?

__iTsMe__
Автор

Can you please do this program using StringTokenizer?

nishka
Автор

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

sanjayagihan
Автор

i thing in strtok function it will be easier but still Great explanation

xturki
Автор

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;
}

justcurious
Автор

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)) {

}
}

i += j;
if (j == word_length) {
counter++;
}
}
}

return counter;
}

deepblackoutlaw
Автор

Hello sir, I come up with the following solution. Do you think it is efficient

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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;
}

aminetakha
Автор

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 and the word be "aa" .
the result of counting should be: 3, not 5, right?

MrJzvoyeur
Автор

--> This is my solution before watching your video

#include <stdio.h>
#include <string.h>

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\n" : "Not found\n", is_found(string, word));

return 0;
}

Abdalrahman_