Saturday, 15 March 2014

Code for Program to get string after user specified position in C Programming

Code for Program to get string after user specified position in C Programming


//xstrchr() fn.


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

void main(){
     char * xstrchr(char *,char);
     charstring[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);
}

No comments:

Post a Comment