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. Function should return position in string.

Posted By: Patti Banks     Category: C Programming     Views: 1952

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. Function should return 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 : BHAUMIKKK    

     Please Give The CHARACTER TO BE FOUND : K

     The character V is at position: 3

************************************************************************
  
Share: 



Patti Banks
Patti Banks author of Program to find first occurrence of a string in another string. Function should return position in string. is from Los Angeles, United States.
 
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!