Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to find first occurrence of a string in another string

Posted By: Ararinda Schmidt     Category: C Programming     Views: 5975

Write a program to find first occurrence of a string in
another string. Function should return position in string.

Code for Program to find first occurrence of a string in another string in C Programming

#include<stdio.h>
#include<conio.h>
void main()
{
char a[21],b;
int i,j,flag=0;
clrscr();
printf("\n Please Give The STRING OF A : ");
scanf("%s",a);
flushall();
printf("\n Please Give The CHARACTER TO B : ");
scanf("%c",&b);
for(i=0,j=0;a[i]!='\0';i++)
{
if(a[i]==b && flag!=1)
{
j=i+1;
flag=1;
}
}
if(flag==1)
printf("The character %c is at position: %d\n", b,j);
else
printf("The character was not found\n");
getch();
}


--------------------------------- OUTPUT ---------------------------------



Please Give The STRING OF A : VIDHI    

Please Give The CHARACTER TO BE FOUND : I


The character I is at position: 2
  
Share: 



Ararinda Schmidt
Ararinda Schmidt author of Program to find first occurrence of a string in another string is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!