Frequency of a given character in a sentence – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Solved Problem #CPP#3243 siteicon   siteicon   siteicon  

Problem Statement - Frequency of a given character in a sentence

Write a program to input a small sentence from the user and ask about frequency of which character is to be found. Print how many times the character occurred in the given string. Do it for a a c-style (null-terminated string). Please note that the character frequency is to be found  irrespective of its lower or upper case.


TC++ #3243


  Related (?) :

Run Output

Input a small sentence:Sports help to live a stress-free life.
Input the character to be found:s
Frequency of given character = 5

char str[80]; cout<<“Input a small sentence:” ; cin.getline(str,80);
A sentence is collected using the getline function of cin object.

char c; cout<<“Input the character to be found:” ; cin>>c;
A single character is collected usin cin.

int cc=0;  //This integer is used for character counting

for(int i=0;str[i]!=’’;i++)  if(toupper(str[i])==toupper(c)) cc++;
Here we traverse all the characters of the sentence till null termination is found. While in loop we compare upper converted character to upper converted character given by the user and if matched we increase the cout. This way we are able to ignore the case.

cout<<“Frequency of given character = ” <<cc<<endl;
The given character cout or in other terms called frequency is printed.



  • If space is given as character here, it can not be found as cin doesn’t allow to move further.
  • If more than 1 character is given then first of the given set of characters is found.
  • For ignoring case, both character in the sentence and character to be matched can be converted to lower case as well.
  • tolower() or toupper() functions do not effect numbers or symbols, so they can also be matched in this program.

Common Errors

  • Student often forget to initialise the counting variable.


sunmitra| Created: 2-Feb-2018 | Updated: 29-Nov-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues