Search This Blog

Loading...

Write a Function XSTRCHR() which scans a string from start to end in search of a character.

/* Write a Function XSTRCHR() which scans a string from start to end in search of a character. */

/*IF THE CHARACTER IS FOUND THEN RETURN IT'S POINTER WITH ITS ADDRESS AND
IF NOT FOUND THEN RETURN NULL VALUE. Code for FUNCTION XSTRCHR() WHICH SCANS A STRING FROM START TO END IN SEARCH OF A CHARACTER in C Programming*/

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

main()
{
    char *a1,*a2,ch;
    char * xstrchr(char *,char);
    clrscr();
    fflush(stdin);
    printf("ENTER THE STRING:->");
    scanf("%[^\n]s",a1);
    fflush(stdin);
    printf("\nENTER THE CHARACTER WHICH YOU WANT TO SEEK:->");
    scanf("%c",&ch);
    a2=xstrchr(a1,ch);
    if (a2==NULL)
        printf("THE CHARACTER %c IS NOT FOUND IN THE STRING.",ch);
    else
        printf("THE CHARACTER %c FOUND AT ADDRESS %u",ch,a2);
    getch();

}

char * xstrchr(char *str,char ch)
{
    int i=0;
    while(str[i]!='\0')
    {
        if(str[i]==ch)
            return(&str[i]);
        i++;
    }
    return NULL;
}

No comments:

Post a Comment