Check if uppercase characters in a string are used correctly or not


Problem Statement

We have given a string ‘str’, containing the alphabetical characters in uppercase or lowercase. We require to check if uppercase characters are used correctly in the string.

Followings are the correct way to use uppercase characters in the string.

  • If only the first character is in uppercase, the other characters are in lowercase.

  • If all characters of a string are in lowercase.

  • If all characters of the string are in uppercase.

Sample examples

Input

"Hello"

Output

"valid"

Explanation

In "Hello", only the first character is in uppercase, and others are in lowercase, so it is a valid string.

Input

'hello'

Output

'valid'

Explanation

All characters are in lowercase in the ‘hello’ string, so it is a valid string.

Input

‘heLLO’

Output

‘Not Valid’

Explanation

In the ‘heLLO’ string, the first character is in lowercase, but the last 3 characters are in uppercase, so the string is not valid.

Approach 1

In this approach, if the first character is in lowercase, we check whether all characters of the string are in lowercase and return a boolean value. If the first character is in uppercase, we check all other characters are either in uppercase or lowercase and return the boolean value.

Algorithm

  • Step 1 − Define the isLower() function, which takes a single character as a parameter and returns the boolean value whether the character is in lowercase. If ‘character-A’ is greater than or equal to 32, it means character in lowercase.

  • Step 2 − Define the isUpper() function like isLower() function and return a boolean value based on whether the character is in uppercase.

  • Step 3 − Define the isValidUpper() function, which checks whether the string contains all valid uppercase characters.

  • Step 4 − In the isValidUpper() function, use the isLower() function, and check if the first character is in lowercase. If yes, check all other characters using the loop and isUpper() function. If any character is in uppercase, return false. Otherwise, return true if all characters are in lowercase.

  • Step 5 − If the first character is in uppercase, we need to check two cases. First, all characters can be in uppercase, or all characters can be in lowercase except the first character.

  • Step 5.1 − Define the ‘totalUpper’ variable and initialize it with 1.

  • Step 5.2 − Count the total number of uppercase characters in the string.

  • Step 5.3 − If the total number of uppercase characters is equal to 1 or the string’s length, it means the string contains valid uppercase characters, return true. Otherwise, return false.

Example

#include <bits/stdc++.h>
using namespace std;
// Check if character c is in lowercase or not
bool isLower(char c){
   return c - 'A' >= 32;
}
// Check if character c is in uppercase or not
bool isUpper(char c){
   return c - 'A' < 32;
}
bool isValidUpperCase(string str){
   int len = str.size();
   // If the first character is in lowercase, check whether all the other characters are in lowercase or not.
   // If not, return false. Otherwise, return true.
   if (isLower(str[0]))  {
      for (int i = 1; i < len; i++) {
         if (isUpper(str[i]))
            return false;
      }
      return true;
   } else {
      // If the first character is in uppercase, find the total number of uppercase characters
      int totalUpper = 1;
      for (int i = 1; i < len; i++){
         if (isUpper(str[i]))
            totalUpper++;
      }
      // if the total number of uppercase characters is equal to the length of the string or 1, return true. Otherwise, return false.
      if (totalUpper == len || totalUpper == 1)
         return true;
      else
         return false;
   }
}
int main(){
   string str1 = "TutorialsPoint";
   string str2 = "tutorialspoint";
   string str3 = "Tutorialspoint";
   string str4 = "TUTORIALSPOINT";
   cout << str1 << " : " << (isValidUpperCase(str1) ? "Valid" : "Not valid") << endl;
   cout << str2 << " : " << (isValidUpperCase(str2) ? "Valid" : "Not valid") << endl;
   cout << str3 << " : " << (isValidUpperCase(str3) ? "Valid" : "Not valid") << endl;
   cout << str4 << " : " << (isValidUpperCase(str4) ? "Valid" : "Not valid") << endl;
   return 0;
}

Output

TutorialsPoint : Not valid
tutorialspoint : Valid
Tutorialspoint : Valid
TUTORIALSPOINT : Valid
  • Time complexity − O(N), as it requires iterating through the string using the loop. The time complexity of the isLower() and isUpper() functions is of O(1).

  • Space complexity − O(1), as it is not using any extra space.

Approach 2

In the below approach, we have optimised the first approach's code. Here, we check whether two adjacent elements of the string are in the same case except the first two characters to check if the string contains valid uppercase characters.

Algorithm

  • Step 1 − Use the for loop to iterate through from the first index of the string to the last index.

  • Step 2 − In the for loop, if the current character is in uppercase and the previous character is in lowercase, return false as it is not a valid string.

  • Step 3 − If the current character is in lowercase and the previous character is in uppercase, follow the steps below.

  • Step 3.1 − Check if the previous character is at the 0th index or the first character in the string, and continue in the for loop.

  • Step 3.2 − Return false if the previous character is not the first character.

Example

#include <bits/stdc++.h>
using namespace std;
bool isValidUpperCase(string str){
   for (int i = 1; i < str.length(); i++){
      // If str[i] is in lower case and str[i-1] is in upper case, handle the case
      if (str[i] - 'A' >= 32 && str[i - 1] - 'A' < 32) {     // If the str[i-1] is the first character, continue the loop. Otherwise, return false.
         if (i - 1 == 0)
            continue;
         return false;
      }
      // If str[i] is in upper case and str[i-1] is in lower case, return false.
      else if (str[i] - 'A' < 32 && str[i - 1] - 'A' >= 32) {
         return false;
      }
   }
   // Return true
   return true;
}
int main(){
   string str1 = "TutorialsPoint";
   string str2 = "tutorialspoint";
   string str3 = "Tutorialspoint";
   string str4 = "TUTORIALSPOINT";
   cout << str1 << " : " << (isValidUpperCase(str1) ? "Valid" : "Not valid") << endl;
   cout << str2 << " : " << (isValidUpperCase(str2) ? "Valid" : "Not valid") << endl;
   cout << str3 << " : " << (isValidUpperCase(str3) ? "Valid" : "Not valid") << endl;
   cout << str4 << " : " << (isValidUpperCase(str4) ? "Valid" : "Not valid") << endl;
   return 0;
}

Output

TutorialsPoint : Not valid
tutorialspoint : Valid
Tutorialspoint : Valid
TUTORIALSPOINT : Valid
  • Time complexity − O(N), as it requires iterating through the string using the loop.

  • Space complexity − O(1), as it is not using any extra space.

Conclusion

Users learned to check if the string contains valid uppercase characters in this tutorial. We have learned two different approaches. In the first approach, we have breakdown the problem into three parts, and in the second approach, we check the character case of adjacent elements. However, the time and space complexity of both codes is similar, but the second approach has more readable code.

Updated on: 18-Jul-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements