In this post, we will write a C++ program to calculate the number of characters in the given string. fist user will enter a sentence and then program will print the value of total number of letters, upper case letters, lower case latters and white space in the sentance.
C++ program to calculate the number of character in the given string.
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
system("cls");
char str[100];
int i,j,n,k,o;
i = 0;
j = 0;
k = 0;
o = 0;
puts("Enter any sentence ");
gets(str);
n = strlen(str);
while(str[i] != ' ')
{
if((str[i] >= 65) && (str[i]<97))
{
j++;
}
if((str[j] >= 97) && (str[i] <= 122))
{
k++;
}
if(str[i] == ' ')
{
o++;
}
i++;
}
std::cout<<"The total number of letters = "<<n<<std::endl;
std::cout<<"The upper case letters = "<<j<<std::endl;
std::cout<<"The lower case letters = "<<k<<std::endl;
std::cout<<"The white spaces = "<<o<<std::endl;
getch();
return 0;
}
Output
Enter any sentence This program is the example of FUNCTION. The total number of letters = 40 The upper case letters = 9 The lower case letters = 24 The white spaces = 6