Monday, November 18, 2024
Google search engine
HomeData Modelling & AIisupper() and islower() and their application in C++

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 argument contains any uppercase letters such as A, B, C, D, …, Z. 

Syntax:
int isupper(int x)

C++




// Program to check if a character is in
// uppercase using isupper()
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
    char x;
    cin >> x;
    if (isupper(x))
        cout << "Uppercase";
    else
        cout << "Not uppercase.";   
    return 0;
}


Output

Not uppercase.

islower() Function: This function is used to check if the argument contains lowercase letters such as a, b, c, d, …, z. 

Syntax:
int islower(int x)

C++




// 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;
}


Output

Not 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 uppercase and vice versa.

Syntax of tolower():

int tolower(int ch);
Syntax of toupper():

int toupper(int ch);

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. 
  2. If lowercase, convert it to uppercase using toupper() function, if uppercase, convert it to lowercase using tolower() function. 
  3. Print the final string.

Implementation:

C++




// 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

Time complexity : O(n) 
Auxiliary Space : O(1)

This article is contributed by Ayush Saxena. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks. 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments