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 and also display position in string

Posted By: Cameron Evans     Category: C Programming     Views: 5256

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 and also display position in 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 : NAYAN    

 Please Give The CHARACTER TO BE FOUND : Y


The character Y is at position: 3


--------------------------------------------------------------------------
  
Share: 



Cameron Evans
Cameron Evans author of Program to find first occurrence of a string in another string and also display position in string is from London, United Kingdom.
 
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!