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

C program to find the last occurrences of a word in a string; Through this tutorial, we will learn how to find the last occurrences of a word in a string using for loop, while loop, and functions in c programs.

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

Using the following programs to find the last occurrences of a word in a string using for loop, while loop, and functions in c:

  • 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 Output 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 Output 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 Output 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 

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *