Skip to content

Instantly share code, notes, and snippets.

@Kinjalrk2k
Created April 23, 2019 08:01
Show Gist options
  • Save Kinjalrk2k/087866e98defb7af6b00db8a19845a06 to your computer and use it in GitHub Desktop.
Save Kinjalrk2k/087866e98defb7af6b00db8a19845a06 to your computer and use it in GitHub Desktop.
Frequency of a word in a sentence using 2D character array
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char sentence[100][100], search_word[100];
int no_of_words = 0, freq = 0;
printf("Enter the no of words: "); // user prompt for:
scanf("%d", &no_of_words); // total no. of words in the sentence
/* now the above 2 lines are very weired! if you ask the user the
the total no. of words before hand: it takes all the fun out of it :( */
printf("Enter the sentence: ");
for(int i=0; i<no_of_words; i++){
scanf("%s", sentence[i]); // IMP: feeding each word in seperate rows of the 2D char array
}
printf("Enter the word to search: "); // user prompt for:
scanf("%s", search_word); // the word to search for
for(int i=0; i<no_of_words; i++){
if(strcmp(sentence[i], search_word) == 0) // if the word is found
freq++; // you know what to do ;)
}
printf("The frequency of %s is %d\n\n", search_word, freq); // output promt
return 0; // satisfaction++
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment