The following program checks, counts and displays the number of times each character has been repeated in a given string by the user. It makes use of two string arrays: a and c, each of whose size is defined as 1000. The input string is stored in a while during checking, the value of a specific character of a is stored in c, during each execution of the loop.
C Program
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000], c[1000];
printf("Enter a string? ");
gets(a), strlwr(a);
int i = 0, j, count, str = strlen(a);
while(i<str)
{
count = 0, c[i] = a[i];
if(c[i]!='\0')
{
for(j=i;j<str;j++)
{
if(a[j]==c[i])
{
count++, a[j] = '\0';
}
}
if(c[i]==' ')
{
printf("\nSpace character is repeated %d times.",count);
}
else
{
printf("\n%c is repeated %d times.",c[i],count);
}
}
i++;
}
}Output
Enter a string? hello world
h is repeated 1 times.
e is repeated 1 times.
l is repeated 2 times.
o is repeated 1 times.
Space is repeated 1 times.
w is repeated 1 times.
r is repeated 1 times.
d is repeated 1 times.
Leave a Reply