In: Computer Science
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.
###Note: the output should print exactly as it is stated in the example if the user enters "full proof" ###
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char phrase[100],duplicate[100];
int count=0,yes=0,i,j,k;
printf("Enter a phrase:");
gets(phrase);/*Reading user input*/
for(i=0;phrase[i]!='\0';i++)
{/*This loop iterates for every charecter*/
if(phrase[i]==' ')
{/*If space is present then we skip
comparing*/
continue;
}
for(j=i+1;phrase[j]!='\0';j++)
{/*This loop iterates for
phrase[i+1] to untill phrase[j]=="\0"*/
if(phrase[i]==phrase[j])
{/*IF
phrase[i]==phrase[j]*/
yes=0;
for(k=0;k<count;k++)
{
if(duplicate[k]==phrase[i])
{/*If the charecter alredy
present in the duplicate array*/
yes=1;
}
}
if(!yes)
{/*If charecter is not present in the
array
then we add it to the duplicate array*/
duplicate[count]=phrase[i];
count++;
/*Here we increase the
count*/
}
}
}
}
if(count==0)
{/*if count is 0 then we print no duplicates*/
printf("No duplicates");
}
else
{/*Else we print the no of duplicates and duplicate
charecters*/
printf("Number of Charecters
Repeated:%d ",count);
printf("Charecters repeated:
");
for(i=0;i<count;i++)
{
if(i!=count-1)
{
printf("%c,",duplicate[i]);
}
else
{
printf("%c",duplicate[i]);
}
}
}
}
Output:
Indentation: