How to Count Uppercase Letters in a String in C++
C++ Program for count upper or lowercase letter of the given string
- Home
- Alphabet
- C++ Program for count upper or lowercase letter of the given string
C++ Program for count upper or lowercase letter of the given string
C++ Program for count upper or lowercase letter of the given string
Contents
- 1 C++ Program for count upper or lowercase letter of the given string
- 1.1 C++ Program for count upper or lowercase letter of the given string
- 1.1.1 Code for count upper or lowercase letter using for loop
- 1.1.2 Code for count upper or lowercase letter using while loop
- 1.1.3 Code for count upper or lowercase letter using do-while loop
- 1.2 Related posts:
- 1.1 C++ Program for count upper or lowercase letter of the given string
In this article, we will discuss the concept of the C++ Program to count uppercase or lowercase letter of the given string
In this post, we are going to learn how to count upper and lower case letters in a given string in C++ programming language
C++ Program for count upper or lowercase letter of the given string
Code for count upper or lowercase letter using for loop
Program 1
#include <iostream> #include <stdio.h> #include <conio.h> using namespace std; int main() { int i; int upper=0,lower=0;//variable declaration char ch[100];//array declaration cout<<"Enter the String:\n"; gets(ch); for(i=0; ch[i]!=0; i++){ if(ch[i]>='A' && ch[i]<='Z'){ upper++; } else if(ch[i]>='a' && ch[i]<='z'){ lower++; } } cout<<"lowercase letters:" <<lower; cout<<"\nuppercase letters:"<<upper; getch(); return 0; }
When the above code is executed, it produces the following result
Enter the string: CPP Language lowercase letters:7 uppercase letters:4
Approach
- Declare a variable as int i;
- Declare and initialize two integer counter-variable as int upper=0 and lower=0;
- Declare a character Array as char ch[100];
- The user asked to enter a string to count upper case and lower case letters
- A for-loop is used to count total upper case and lower case of the given string
- It is initialized as i=0, and checks condition whether ch[i]!=0;, and executes the loop until the given condition becomes true
- Use an if statement to test upper case , if the test expression is true, upper becomes upper + 1 (upper=upper+1)
- When the if-statements is false, The control moves to else if and checks the test expression of else-if
- If the test expression of else-if is true, lower becomes lower + 1(lower =lower +1)
- If the test expression of else if is false, control terminates from the loop
- Finally, the program displays the number of upper case and lowercase letters of the given string.
Code for count upper or lowercase letter using while loop
The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using while loop in C++ language
Program 2
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { int i; int upper=0,lower=0; char ch[100]; cout<<"Enter the String:\n"; gets(ch); i=0; while(ch[i]!=0){ if(ch[i]>='A' && ch[i]<='Z'){ upper++; } else if(ch[i]>='a' && ch[i]<='z'){ lower++; } i++; } cout<<"lowercase letters: "<<lower; cout<<"\nuppercase letters: "<<upper; getch(); return 0; }
When the above code is executed, it produces the following result
Enter the string: Count Upper Lower lowercase letters:12 uppercase letters:3
Approach
- Declare a variable as int i;
- Declare and initialize two integer counter-variable as int upper=0 and lower=0;
- Declare a character Array as char ch[100];
- The user asked to enter a string to count upper case and lower case letters
- A while-loop is used to count total upper case and lower case of the given string
- It is initialized as i=0, and checks condition whether ch[i]!=0;, and executes the loop until the given condition becomes true
- Use an if statement to test upper case , if the test expression is true, upper becomes upper + 1 (upper=upper+1)
- When the if-statements is false, The control moves to else if and checks the test expression of else-if
- If the test expression of else-if is true, lower becomes lower + 1(lower =lower +1)
- If the test expression of else if is false, control terminates from the loop
- Finally, the program displays the number of upper case and lowercase letters of the given string.
Code for count upper or lowercase letter using do-while loop
The program allows to enter a String and it counts and displays whether the number of upper case and lower case letters of the given string using do-while loop in C++ language
Program 3
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { int i; int upper=0,lower=0; char ch[100]; cout<<"Enter the String:\n"; gets(ch); i=0; do{ if(ch[i]>='A' && ch[i]<='Z'){ upper++; } else if(ch[i]>='a' && ch[i]<='z'){ lower++; } i++; } while(ch[i]!=0); cout<<"lowercase letters: "<<lower; cout<<"\nuppercase letters: "<<upper; getch(); return 0; }
When the above code is executed, it produces the following result
Enter the string: UpperCase and LowerCase lowercase letters:12 uppercase letters:3
Approach
- Declare a variable as int i;
- Declare and initialize two integer counter-variable as int upper=0 and lower=0;
- Declare a character Array as char ch[100];
- The user asked to enter a string to count upper case and lower case letters
- A while-loop is used to count total upper case and lower case of the given string
- It is initialized as i=0, and checks condition whether ch[i]!=0;, and executes the loop until the given condition becomes true
- Use an if statement to test upper case , if the test expression is true, upper becomes upper + 1 (upper=upper+1)
- When the if-statements is false, The control moves to else if and checks the test expression of else-if
- If the test expression of else-if is true, lower becomes lower + 1(lower =lower +1)
- If the test expression of else if is false, control terminates from the loop
- Finally, the program displays the number of upper case and lowercase letters 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
the operator in C++ language
Data type in C++ language
Variable in C++ language
How to Count Uppercase Letters in a String in C++
Source: https://code4coding.com/c-program-for-count-upper-or-lowercase-letter-of-the-given-string/
0 Response to "How to Count Uppercase Letters in a String in C++"
Post a Comment