C Program to Find first occurrence of char with starting position

C Program to Find first occurrence of char with starting position.Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int First_Occurrence_Char(char [],char ,int);
main()
{
      char text[30],searchChar;
      int position,start;
      
       printf("\nProgram to Find first occurrence of char with starting position \n");
       printf("\n------------------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);
       
       printf("\nSearched Char : ");
       scanf("%c",&searchChar);
       
       printf("\nStart From : ");
       scanf("%d",&start);
       
       
       position=First_Occurrence_Char(text,searchChar,start);
      
       printf("\nCharecter '%c'  in String '%s' starting at %d found at position %d ",searchChar,text,start,position);
       
       
       getch();
       
} 
      
           
int First_Occurrence_Char(char a[],char b,int start)

{
     
     int i;
     char *p;
     
     for(i=start;i<strlen(a);i++) 
     {
         p=&a[i];       
      
        
         
         if(*p==b)
         {
                  
                    break;
                 
         }
               
                              
     }    
     if(i==strlen(a))
     {
       return -1;                
     }
     else
     {
         return i;
     }
     
}

Output

occurrencewithstart

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s