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 and also position in string using strchr function from string.h

Posted By: Raymund Fischer     Category: C Programming     Views: 10608

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 and also position in string using 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 : NAYAN

 Please Give The CHARACTER TO B : Y


The character Y is at position: 3


--------------------------------------------------------------------------
  
Share: 



Raymund Fischer
Raymund Fischer author of Program to find first occurrence of a character in a string and also position in string using strchr function from string.h is from Frankfurt, Germany.
 
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!