Tuesday 17 March 2015

To count no. of occurence of a character in a string Using C


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

void main()
{
char str[80], ch ;
int i = 0, count = 0 ;
clrscr() ;
printf("Enter the text : \n\n") ;
gets(str) ;
printf("\nEnter the character to be search : ") ;
scanf("%c", &ch) ;
while(str[i] != '\0')
{
if(ch == str[i])
count++ ;
i++ ;
}
printf("\nThe character %c appears %d times in the text", ch,
count) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter the text :
Anna University
Enter the character to be search : n
The character n appears 3 times in the text

No comments:

Post a Comment