This program calculates the frequency of characters in a string, that is to say, the characters are present, how many times in a row. For example, the string "code" each of the character 'c', 'o', 'd' and 'e' appeared once. Considering all lowercase, ignoring other characters (special characters). You can easily program this large and special characters to deal with.
#include <stdio.h>
#include <string.h>
int main()
{
char string[100], ch;
int c = 0, count[26] = {0};
printf("Enter a string\n");
gets(string);
while ( string[c] != '\0' )
{
/* Considering characters from 'a' to 'z' only */
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;
c++;
}
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char string[100], ch;
int c = 0, count[26] = {0};
printf("Enter a string\n");
gets(string);
while ( string[c] != '\0' )
{
/* Considering characters from 'a' to 'z' only */
if ( string[c] >= 'a' && string[c] <= 'z' )
count[string[c]-'a']++;
c++;
}
for ( c = 0 ; c < 26 ; c++ )
{
if( count[c] != 0 )
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}
return 0;
}
No comments:
Post a Comment