asked 152k views
2 votes
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print "No character is repeated" For example: If the phrase is "full proof" then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10.

1 Answer

2 votes

Answer:

#include <stdio.h>

int main() {

char phrase[10];//declaring a string.

printf("Enter the phrase\\");

scanf("%[^\\]s",phrase);//taking a phrase as an input with spaces.

int a[256]={0};//count array.

for(int i=0;phrase[i]!='\0';i++)

{

a[phrase[i]]++;//updating the count.

}

for(int i=0;i<256;i++)

{

if(a[i]>1)//printing the characters having count greater than 1.

{

printf("%c ",i);

}

}

return 0;

}

Output:-

Enter the phrase

full proof

f l o

Step-by-step explanation:

In the code written above I have declared a string phrase and then taking input from the user in the string phrase with white spaces.Then I have created a count array for the storing the frequency of the characters and then looping over the array a and if there any element is greater than 1 means frequency is greater than 1 then printing that character.

answered
User Jacobkim
by
7.8k points

No related questions found