C Programming Blog

 
 
 

Image

//xstrchr() fn.


#include <stdio.h>
#include <conio.h>

void main(){
     char * xstrchr(char *,char);
     char string[15];
     char *ptr,c;
    clrscr();
    printf(\"Enter a string : \");
    scanf(\"%[^\\n]s\",string);
    printf(\"Enter search character : \");
    flushall();
    scanf(\"%c\",&c);
    ptr = xstrchr(string, c);
    printf(\"\\nOutput of function is : %s\\n\",ptr);
    if (ptr)
       printf(\"The character %c is at position: %d\\n\", c, ptr-string);
    else
       printf(\"The character was not found\\n\");
    getch();
}

char * xstrchr(char *str,char ch){
       int i;
       for(i=0;str[i];i++){
       if(str[i]==ch)
          return ((str+i)+1);
       }
       return(0);
}

 
 
Didn't find what you were looking for? Find more on Program to get string after user specified position