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

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

Programs to Find First Occurrence of a Character in a String in C

Let’s use the following programs to find the first occurrences of a character in a string using for loop, while loop, recursion, and functions in c:

  • C Program to Find First Occurrence of a Character in a String using For Loop
  • C Program to Find First Occurrence of a Character in a String using While Loop
  • C Program to Find First Occurrence of a Character in a String Using Recursion
  • C Program to Find First Occurrence of a Character in a String using Function

C Program to Find First Occurrence of a Character in a String using For Loop

#include <string.h>

int main()
{
    char s[1000],c;
    int i,n,k=0;

    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();

    for(i=0;s[i];i++)
    {
    	if(s[i]==c)
    	{
		  k=1;
    	  break;
		}
 	}
    if(k)
 	    printf("character  %c  is first occurrence at location:%d ",c,i);
    else
        printf("character is not in the string ");



    return 0;
}

The Output of the above c program; as follows:

Enter  the string : hello programmer
Enter character to be searched: e
character  e  is first occurrence at location:1 

C Program to Find First Occurrence of a Character in a String using While Loop

/* C Program to find First Occurrence of a Character in a String */

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

int main()
{
  	char str[100], ch;
  	int i, Flag;
  	i = Flag = 0;

  	printf("\n Please Enter any String :  ");
  	gets(str);

  	printf("Enter character to be searched: ");

  	scanf("%c", &ch);

  	while(str[i] != '\0')
  	{
  		if(str[i] == ch)
		{
  			Flag++;
			break;
 		}
 		i++;
	}
    if(Flag == 0)
  	{
  		 printf("character is not in the string ");
	}
	else
	{
	  printf("character  %c  is first occurrence at location:%d ",ch,i);

	}
  	return 0;
}

The Output of the above c program; as follows:

Please Enter any String :  hello programer
Enter character to be searched: e
character  e  is first occurrence at location:1 

C Program to Find First Occurrence of a Character in a String Using Recursion

 #include <string.h>


int check(char *s,char c)
{
    static int i;
    if(!s[i])
    {
        return -1;
	}
	else
	{
		 if(s[i]==c)
		 return i;
		 i++;
		 check(s,c);
	}





}
int main()
{
    char s[1000],c;
    int n;

    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();
    n=check(s,c);


    if(n>-1)
 	    printf("character  %c  is first occurrence at location:%d ",c,n);
    else
        printf("character is not in the string ");
        return 0;
 }

The Output of the above c program; as follows:

Enter  the string : welcome to programming world
Enter character to be searched: w
character  w  is first occurrence at location:0 

C Program to Find First Occurrence of a Character in a String using Function

#include <string.h>

int check(char *s,char c)
{
    int i,k=0;
      for(i=0;s[i];i++)
    {
    	if(s[i]==c)
    	{
		  k=1;
    	  break;
		}
 	}
   if(k)
   return i;
   else
   return -1;


 }
int main()
{

   char s[1000],c;
    int n;

    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();
    n=check(s,c);


    if(n>-1)
 	    printf("character  %c  is first occurrence at location:%d ",c,n);
    else
        printf("character is not in the string ");



    return 0;



}

The Output of the above c program; as follows:

Enter  the string : Hello
Enter character to be searched: o
character  o  is first occurrence at location:4 

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 *