C program for counting Occurrence of any character in given String.


#include<stdio.h>
#include<string.h>
int main()
{
char str[150];
char ch;
int flag=0,j=0,count=0;
printf("Enter a String: ");
fgets(str,150,stdin);
printf("Enter a Character: ");
ch=getchar();
while(str[j]!='\0')
{
if(str[j]==ch)
{
flag=1;
count+=1;
}
j++;
}
if(flag==1)
{
printf("Character %c is occured in String %d times.",ch,count);
}
else
{
printf("Character %c is not present in String",ch);
}
return 0;
}

***Input && Output***



Post a Comment

0 Comments