Home »
C++ Programs
C++ program to check if the string is in lowercase using class
Submitted by Shubh Pachori, on September 06, 2022
Problem statement
Given a string, we have to check if the string is in lowercase using the class and object approach.
Example:
Input:
Enter String: SHUBH
Output:
String is not in lowercase!
C++ code to check if the string is in lowercase using the class and object approach
#include <iostream>
using namespace std;
// create a class
class String {
// private data member
private:
char str[30];
// public member functions
public:
// getString() function to store string
void getString() {
cout << "Enter String: ";
cin.getline(str, 30);
}
// isLowercase() function to check
// if the string is in lowercase
void isLowercase() {
// initializing int type variables
// to perform operations
int index, check = 0;
// for loop to traverse the whole string
for (index = 0; str[index]; index++) {
// if condition to check if the character at
// index is alphabet or not
if ((str[index] >= 'A' && str[index] <= 'Z') ||
(str[index] >= 'a' && str[index] <= 'z') || (str[index] == 32)) {
// if condition to check if the character at
// index is in lowercase or not
if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] == 32)) {
check++;
} else {
check = 0;
break;
}
} else {
check = 0;
break;
}
}
if (check != 0) {
cout << "String is in lowercase!" << endl;
} else {
cout << "String is not in lowercase!" << endl;
}
}
};
int main() {
// create an object
String S;
// calling getString() function
// to insert string
S.getString();
// calling isLowercase() function to
// check the string
S.isLowercase();
return 0;
}
Output
RUN 1:
Enter String: hello world
String is in lowercase!
RUN 2:
Enter String: includehelp
String is in lowercase!
Explanation
In the above code, we have created a class String, one char type array data member str[30] to store the string, and public member functions getString() and isLowercase() to store the string and to check if the string is in lowercase or not.
In the main() function, we are creating an object S of class String, reading a string by the user using the function getString(), and finally calling the isLowercase() member function to check the string if it is in lowercase or not. The isLowercase() function contains the logic to check if the string is in lowercase or not and printing the result.
C++ Class and Object Programs (Set 2) »