To count no. of occurence of a character in a string
Program:
#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();
}
Output:
Enter the text :
Anna University
Enter the character to be search : n
The character n appears 3 times in the text
-
UpdatedDec 31, 2019
-
Views6,944
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order