In this article, we will discuss the concept of the C program: find the frequency of the given character in a string
In this post, we are going to learn how to find the frequency of the given character in the string in C programming language
The program allows the user to enter a String and then it finds the frequency of the given character in a string using for loop in C programing language
program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100],ch; int fCount=0,i; printf("Enter a string!\n"); fgets(str,sizeof(str),stdin); printf("Enter a character to find its frequency\n"); scanf("%c",&ch); for(i=0; str[i]!='\0'; i++){ if(ch==str[i]) fCount++; } printf("The frequency of %c is= %d",ch,fCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string C language Enter a character to find its frequency a The frequency of a is=2
The program allows the user to enter a String and then it finds the frequency of the given character in a string using while loop in C programing language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char str[100],ch; int fCount=0,i; printf("Enter a string!\n"); fgets(str,sizeof(str),stdin); printf("Enter a character to find its frequency\n"); scanf("%c",&ch); i=0; while(str[i]!='\0'){ if(ch==str[i]) fCount++; i++; } printf("The frequency of %c is= %d",ch,fCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string! C programming language Enter a character to find its frequency g The frequency of g is=4
The program allows the user to enter a String and then it finds the frequency of the given character in a string using do-while loop in C programing language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { char str[100],ch; int fCount=0,i; printf("Enter a string!\n"); fgets(str,sizeof(str),stdin); printf("Enter a character to find its frequency\n"); scanf("%c",&ch); i=0; do{ if(ch==str[i]) fCount++; i++;} while(str[i]!='\0'); printf("The frequency of %c is= %d",ch,fCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string! C and Java are easy languages Enter a character to find its frequency a The frequency of g is=7
Approaches
Suggested for you
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…