C program to search all occurrences of a word in a given string – In this article, we will particularise the different ways to search all occurrences of a word in a given string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The ways used in this specific piece are as follows:
- Using Standard Method
- Using Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
The image uploaded above states that first, you need to enter the string concerned.
The string entered here is as follows:
“hello world hello world hello world”
In the next step, you need to specify what is the word that you want to be searched.
The word “world” is listed for the search part.
Thus, it is seen that the word “world” is spotted at locations 6, 18 and 30.
Thus, the numerous means to do so in C programming are as follows:
Using Standard Method
- The function gets(s) reads the entered string and it stores the string in the character arrays[].
2) The gets(w) function reads the entered word and saves in the character array w[].
3) For loop iterates through the string and finds the index values of the white spaces, stores the index values in the integer array a[].
4) The for loop iterates through the white spaces of the string with the structure for(i=0; i<k;i++)
a) Initialize n=a[i]-j i.e first white space index-j
b) If the length of the word is equal to n
b.1) Then t=0, compare the first element of the word with the first element of the string as s[l+j]==w[l]. If matched then increase the ‘t’ value. Repeat this for all elements of the word by increasing l value. If t=length of the word then increase the ‘found’ value by 1 and print the location of the occurrence of the word in the string i.e. j.
If the given word has not occurred before the first whitespace then initialize j=a[i]+1 and go to the next index of the whitespace by increasing i value. Repeat until i<k.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> #include <string.h> int main() { char s[1000],w[1000]; int n,a[1000],i,j,k=0,l,found=0,t=0; printf("Enter the string : "); gets(s); printf("Enter word to be searched: "); gets(w); for(i=0;s[i];i++) { if(s[i]==' ') { a[k++]=i; } } a[k++]=i; j=0; for(i=0;i<k;i++) { n=a[i]-j; if(n==strlen(w)) { t=0; for(l=0;w[l];l++) { if(s[l+j]==w[l]) { t++; } } if(t==strlen(w)) { found++; printf("word '%s' is occurred at location=%d \n",w,j); } } j=a[i]+1; } } |
Output:
1 2 3 4 5 |
Enter the string: hello world hello world hello world Enter word to be searched: world word 'world' is occurred at location=6 word 'world' is occurred at location=18 word 'world' is occurred at location=30 |
Using Function
- The main() call the check(char *s, char *w) function to find all occurrences of the word in the given string.
2) The function
a) Iterates through the string using for loop(i=0;s[i];i++) and finds the index values of the white spaces and place the index values in the integer array a[].
b) j=0, Iterate through the white spaces until i<k,
Initialize n=a[i]-j
b.1) If the length of the word is equal to the first index value of white space then compare each letter of the word with the elements of the string as s[i+l]==w[l] by increasing l value. If matched then t++. If ‘t’ is equal to the length of the word then print the location of occurrence of the word.
If the word has not occurred before the first whitespace then initialize j=a[i]+1 and go for next index of the white space. Repeat the loop until i<k.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <stdio.h> #include <string.h> void check(char *s,char *w) { int n,a[1000],i,j,k=0,l,found=0,t=0; for(i=0;s[i];i++) { if(s[i]==' ') { a[k++]=i; } } a[k++]=i; j=0; for(i=0;i<k;i++) { n=a[i]-j; if(n==strlen(w)) { t=0; for(l=0;w[l];l++) { if(s[l+j]==w[l]) { t++; } } if(t==strlen(w)) { found++; printf("word '%s' is occurred at location=%d \n",w,j); } } j=a[i]+1; } } int main() { char s[1000],w[1000]; int n; printf("Enter the string : "); gets(s); printf("Enter word to be searched: "); gets(w); check(s,w); } |
Output:
1 2 3 4 |
Enter the string: hello world welcome to cbeginners welcome to programming Enter word to be searched: to The word 'to' is occurred at location=20 The word 'to' is occurred at location=42 |