C Program to Find First Occurrence of a Word in a String

In this tutorial, i am going to show you how to find the last occurrences of a word in a string with the help of for loop, while loop, and functions in c programs.

All C Programs to Find Last Occurrence of a Word in a String

  • C Program to Find Last Occurrence of a Word in a String using For Loop
  • C Program to Find Last Occurrence of a Word in a String using While Loop
  • C Program to Find Last Occurrence of a Word in a String using Function

C Program to Find Last Occurrence of a Word in a String using For Loop

/* C Program to Find First Occurrence of a Word in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100], word[100];
  	int i, j, Flag;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
    printf("Enter word to be searched: ");
  	gets(word);
	     	   	
  	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == word[0])
		{
			Flag = 1;
			for(j = 0; word[j] != '\0'; j++)
			{
				if(str[i + j] != word[j])
				{
					Flag = 0;
					break;
				}
			}	
		}
		if(Flag == 1)
		{
			break;
		} 
	}
	if(Flag == 0)
  	{
  		printf("\n Sorry!! We haven't found the Word '%s' ", word);
	}
	else
	{
		printf("\n We found '%s' at position %d ", word, i + 1);
	}	
	
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String :  hello my dear friend
Enter word to be searched: dear
We found 'dear' at position 10 

C Program to Find Last Occurrence of a Word in a String using While Loop

/* C Program to Find First Occurrence of a Word in a String */
 
#include <stdio.h>
 
int main()
{
  	char str[100], word[100];
  	int i, j, Flag;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
    printf("Enter word to be searched: ");
  	gets(word);
	     	   	
  	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == word[0])
		{
			Flag = 1;
			for(j = 0; word[j] != '\0'; j++)
			{
				if(str[i + j] != word[j])
				{
					Flag = 0;
					break;
				}
			}	
		}
		if(Flag == 1)
		{
			break;
		} 
	}
	if(Flag == 0)
  	{
  		printf("\n Sorry!! We haven't found the Word '%s' ", word);
	}
	else
	{
		printf("\n We found '%s' at position %d ", word, i + 1);
	}	
	
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String :  welcome to programming world
Enter word to be searched: to
We found 'to' at position 9 

C Program to Find Last Occurrence of a Word in a String using Function

#include <stdio.h>
#include <string.h> 
int 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=1;
				break;
		}
		j=a[i]+1;
	}
 	if(found)
	return j;
	 
    return -1;
 
	
}
 
int main()
{
    char s[1000],w[1000]; 
	int n; 
 
    printf("Enter  the string : ");
    gets(s);
    printf("Enter word to be searched: ");
    gets(w);
    n=check(s,w);
    if(n>=0)
     printf("word %s  is first occurred at location:%d ",w,n);
    else
     printf("word is not occurred in the string ");
}     

The result of the above c program; as follows:

Enter  the string : hello dear good morning
Enter word to be searched: good
word good  is first occurred at location:11 

More C Programming Tutorials

Leave a Comment