String

C++ program: find frequency of a character in the string

C++ program: find the frequency of a character in the string

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

Find the frequency of a character in a string

Code to find the frequency of  the given character in the string

find the frequency of  the given character in the string using for loop

The program allows the user to enter a string and a character then it finds the frequency of the given character in the string using for loop in C++ programing language

program 1

#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
    char str[100],ch;
    int fCount=0,i;

    cout<<"Enter a string!\n";
    gets(str);
    cout<<"Enter a character to find its frequency\n";
   cin>>ch;

   for(i=0; str[i]!='\0'; i++){
    if(str[i]==ch){ fCount++;
    }
   }
   cout<<"The frequency of "<<ch<<" is= "<<fCount;
   getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string
code for coding
Enter a character to find its frequency
o
The frequency of o is= 3

 

Find the frequency of a character in a string using while loop

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 <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
    char str[100],ch;
    int fCount=0,i;

    cout<<"Enter a string!\n";
    gets(str);
    cout<<"Enter a character to find its frequency\n";
   cin>>ch;
i=0;
   while(str[i]!='\0'){
    if(str[i]==ch){ fCount++;
    }
    i++;
   }
   cout<<"The frequency of "<<ch<<" is= "<<fCount;
   getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string
Cpp language supports OOPconcept
Enter a character to find its frequency
p
The frequency of p is= 5

 

Find the frequency of a character in a string using do-while loop

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 <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
    char str[100],ch;
    int fCount=0,i;

    cout<<"Enter a string!\n";
    gets(str);
    cout<<"Enter a character to find its frequency\n";
   cin>>ch;
i=0;
   do{
    if(str[i]==ch){ fCount++;
    }
    i++;
   }while(str[i]!='\0');
   cout<<"The frequency of "<<ch<<" is= "<<fCount;
   getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter a string
cpp is a basic language
Enter a character to find its frequency
a
The frequency of a is= 4

 

Approaches

  1. Declare a character array char str[100] and character variable char ch;
  2. Declare and initialize integer variables as frequency=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. The user asked to enter a character
  6. The given character is stored in the variable ch;
  7. A loop(for, while and do-while) is used to count frequency of the character in the given string.
  8. It is initialized as i=0, checks the condition whether str[i]!=’\0′); and executes the loop until the given condition becomes true
  9. Use an if condition to test if(ch==str[i]), when the if statements is true,  The frequency becomes frequency + 1( frequency = frequency +1);
  10. When it is false, terminates the loop
  11. Finally, the program displays the frequency of the given character of the given string

 

 

Suggested for you

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

if statements in C++ language

 

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 program: find the frequency of given character in a string
Python program: find the frequency of a character in a string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago