Counting Characters in a String in C++

Introduction

This is a straightforward C++ program that counts the number of characters in a string typed by the user. The program uses cin.get() to read the string and cout.put() to display the characters. It keeps track of the character count using a variable and shows the total count at the end.

C++

Key Concepts

Before diving into the code, let's understand a few key concepts:

  • C++ Input/Output Streams: C++ provides the iostream library, which includes the cin and cout objects for handling input and output operations. These objects are part of the standard input/output stream class.
  • Character Input: The cin.get() function is used to read a single character from the input stream. It takes a single argument, which is the variable where the character will be stored.
  • Character Output: The cout.put() function is used to display a single character on the output stream. It takes a single argument, which is the character to be displayed.
  • Looping: The while loop is used to repeatedly read characters until a newline character ('\n') is encountered. This allows the program to read an entire string entered by the user.

Code Structure

The code follows a simple structure:
  • Header: The necessary header file iostream is included to use the input/output stream objects.
  • Main Function: The main() function is the entry point of the program.
  • Variable Declaration: The count variable is declared to keep track of the number of characters in the string, and the c variable is declared to store each character read from the input.
  • User Input: The program prompts the user to enter a string using the cout object and reads the first character using the cin.get() function.
  • Loop: The while loop is used to iterate until a newline character ('\n') is encountered.
  • Character Output: Inside the loop, the program displays the character using the cout.put() function and increments the count variable.
  • Output: After the loop ends, the program outputs the total count of characters using the cout object.
  • Return: The main() function returns 0 to indicate the successful execution of the program.

Code Examples

Here's an example of how the program works:
#include<iostream>
using namespace std;
int main()
{
	int count=0;
	char c;
	cout<<"Enter String :";
	cin.get(c);
	while(c!='\n')
	{
		cout.put(c);
		count++;
		cin.get(c);
	}
	cout<<endl;
	cout<<"Number of characters is: "<<count<<endl;
	return 0;
}
Input:
Enter String :Hello World
Output:
Hello World
Number of characters is: 11
In this example, the user enters the string "Hello World". The program reads each character one by one and displays it on the console. After the loop ends, the program outputs the total count of characters, which is 11 in this case.

Conclusion

This code demonstrates a simple way to count the number of characters in a string using C++. By utilizing the cin.get() and cout.put() functions, the program reads and displays characters while keeping track of the count. This can be useful in various applications where character counting is required, such as text processing or data validation.

Learn More...

Next Post Previous Post
No Comment
Add Comment
comment url