isupper() and islower() and their application in C++
In C++, isupper() and islower() are predefined functions used for string and character handling.cstring.h is the header file required for string functions and cctype.h is the headerfile required for character functions.
isupper() Function
This function is used to check if the arguement contains any uppercase letters such as A, B, C, D……….Z.
Syntax int isupper(int x)
// Program to check if a character is in // uppercase using islower() #include <iostream> #include <cctype> using namespace std; int main() { char x; cin >> x; if (isupper(x)) cout << "Uppercase"; else cout << "Not uppercase."; return 0; } |
Input : A Output : Uppercase
Input : a Output : Not uppercase
islower() Function
This function is used to check if the arguement contains lowercase letters such as a, b, c, d, ……..z.
Syntax int islower(int x)
// Program to check if a character is in // lowercase using islower() #include <iostream> #include <cctype> using namespace std; int main() { char x; cin >> x; if (islower(x)) cout << "Lowercase"; else cout << "Not Lowercase."; return 0; } |
Input:A Output : Not Lowercase
Input : a Output : Lowercase
Application of islower(), isupper(), tolower(), toupper() function.
Given a string, task is to convert the characters in the string into opposite case, i.e. if a character is in lowercase, we need to convert it into uppercade and vice versa.
Examples:
Input : GeekS Output :gEEKs Input :Test Case Output :tEST cASE
1. Traverse the given string character by character upto its length, check if character is in lowercase or uppercase using predefined function.
3. If lowercase, convert it to uppercase using toupper() function, if uppercase, convert it to lowercase using tolower() function.
4. Print the final string.
// C++ program to toggle cases of a given // string. #include <iostream> #include <cstring> using namespace std; // function to toggle cases of a string void toggle(string& str) { int length = str.length(); for (int i = 0; i < length; i++) { int c = str[i]; if (islower(c)) str[i] = toupper(c); else if (isupper(c)) str[i] = tolower(c); } } // Driver Code int main() { string str = "GeekS"; toggle(str); cout << str; return 0; } |
Output:
gEEKs
This article is contributed by Ayush Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- unordered_multimap and its application
- C++ bitset and its application
- is_permutation() in C++ and its application for anagram search
- isspace() in C/C++ and its application to count whitespace characters
- Oracle Interview | Set 8 (On Campus for Application Developer)
- VMware Interview Experience | Set 5 (On-Campus for IT Application Developer)
- Minimum operations required to convert a binary string to all 0s or all 1s
- String Range Queries to find the number of subsets equal to a given String
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Swap characters in a String
- Sort an array of strings according to string lengths using Map
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Round the given number to nearest multiple of 10 | Set-2



