Program To count Number Of Occurrence
Of Characters In A StringLogic Applied:-
User Enters A String ,After that A While Loop Is For Looping Till Last Character In A String,
In The Next Line An if condition is for checking characters from A To Z,Now We Simply Count Characters /by Increment Operator
And Finally Print The Character Whose Value is At-least 1
Source Code:-
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c = 0, count[26] = {0};
printf("Enter a string\n");
gets(string);
while (string[c] != '\0')
{ /** Considering characters from 'a' to 'z' only ignoring others */
if (string[c] >= 'a' && string[c] <= 'z')
count[string[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{ /** Printing only those characters whose count is at least 1 */
if (count[c] != 0)
printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
}
return 0;
}
Thank You For Be Here:-
Please Share This Post
Comments
Post a Comment