PROGRAM FOR SEARCHING A CHARACTER IN A STRING

#include
#include

void main()
{
char str[20],ch;
int cnt=0,i;
clrscr();

printf("\n\n\n\t\t Program for searching a character in a string");

printf("\n\n\n\t\t\t Enter a string = ");
scanf("%s",&str);

printf("\n\t\t\t Enter the character to search = ");
flushall();
scanf("%c",&ch);

for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
cnt++;

}

if(cnt==0)
printf("\n\n\t\t\t The character '%c' is not found in the string...",ch);
else
printf("\n\n\t\t\t The character '%c' is occuring %d times in the string.",ch,cnt);


getch();

}

No comments:

Post a Comment