# C PROGRAMS
String in C
➤ Read Display String in C
➤ 2D Array of Strings in C
➤ Find Length of String
➤ Copy Two Strings in C
➤ Concatenate Two Strings
➤ Compare two strings
➤ Reverse a String in C
➤ Palindrome string in C
➤ Uppercase to Lowercase
➤ Lowercase to Uppercase
➤ Remove all characters in String except alphabet
➤ Find Frequency of Characters
➤ Count Number of Words
➤ Count lines, words, & characters
➤ Count Vowel, Consonant, Digit, Space, Special Character
➤ String Pattern in C
➤ Copy Input to Output
Here we will write a C Program to find the frequency of characters in a string. For example in the word “Programming”, the frequency of “m” is 2, because m comes 2 times.
#include<stdio.h>
int main()
{
char ch, str[200];
int i, frequency=0;
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to check its frequency: ");
scanf("%c",&ch);
for(i=0;str[i]!='\0';i++)
{
if(ch==str[i]) frequency++;
}
printf("The frequency of %c is= %d",ch,frequency);
return 0;
}
Output:-
Enter string: Anyone can write code that a computer can understand, Good programmers write code that humans can understand.
Enter a character to check its frequency: a
The frequency of a is= 10
Enter string: Talk is cheap, show me the code.
Enter a character to check its frequency: z
The frequency of z is= 0
C program to find frequency of characters in a string, In this program we take input from user using fgets() method. The fgets() method read the string and also add newline character (conversion of the enter key) into the string. Later we take character to search the frequency and store it in the variable ch. Using if statement every character of string is checked. To count the frequency of character another variable frequency is used. Whenever the character of string is equal to the searching character, then the frequency is incremented. At the end of the string we get the final result and the result is displayed to the screen.
In this program for different methods of inputs of string, you may get different behaviours of the program so be careful.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
More C programming Examples on String
- Remove all characters in a string except alphabet
- Find frequency of characters in string
- C program to count the number of words in a string
- Count lines, words, and characters in a given text
- Vowel, consonant, digit, space, special character Count
- String pattern programs in C programming language
- Copy Input to Output Using C programming
- Find the length of String
- Copy two Strings in C
- Concatenate Two Strings
- Compare two strings
- Reverse a String in C
- Palindrome string in C
- Uppercase to a lowercase character
- Lowercase to uppercase character
- C program to search a string in the list of strings
- Sort Elements in Lexicographical Order (Dictionary Order)