Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to find first occurrence of a character in a string. Function should return character's position in string. (Use strchr function from string.h

Posted By: Cathy Spencer     Category: C Programming     Views: 5956

Write a program to find first occurrence of a character in a string. Function should return character's position in string. (Use strchr function from string.h)

Code for Program to find first occurrence of a character in a string. Function should return character's position in string. (Use strchr function from string.h in C Programming

#include<stdio.h>
    #include<conio.h>
    #include<string.h>
    void main()
    {
        char a[20],b;
        char *ptr;
        clrscr();
        printf("\n Please Give The STRING OF A : ");
        scanf("%s",a);
        flushall();
        printf("\n Please Give The CHARACTER TO B : ");
        scanf("%c",&b);

        ptr = strchr(a, b);

        if (ptr)
           printf("The character %c is at position: %d\n", b, ptr-a+1);
        else
           printf("The character was not found\n");
        getch();
    }


******************************OUTPUT ****************************************


     Please Give The STRING OF A : World

     Please Give The CHARACTER TO B : r


        The character Y is at position: 3


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



Cathy Spencer
Cathy Spencer author of Program to find first occurrence of a character in a string. Function should return character's position in string. (Use strchr function from string.h is from Wichita , United States. Cathy Spencer says

 
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!