Check if uppercase characters (Capital letters) in a string are used correctly or not | Set 2
Last Updated :
10 Feb, 2022
Given a string S consisting of uppercase and lowercase letters, the task is to check if uppercase characters are used correctly in the given string or not. Correct usage of uppercase characters are as follows:
- All characters in the string are in uppercase. For example, “GEEKS”.
- None of the characters are in uppercase. For example, “geeks”.
- Only the first character is in uppercase. For example, “Geeks”.
Examples:
Input: S = “Geeks”
Output: Yes
Explanation: Only the first character of the string is in uppercase and all the remaining characters are in lowercase.
Input: S = “GeeksForGeeks”
Output: No
Approach: The given problem has already been discussed in Set 1 of this article. This article suggests a different and easy to implement approach which is based on the following two observations:
- If the current letter is a capital alphabet and the previous letter was a small alphabet, return false.
- If the current letter is a small alphabet and the previous letter is a capital alphabet and the previous letter is not the 1st character of the string, return false.
If the complete string has been traversed without violating any of the above two cases, return true.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
bool detectUppercaseUse(string word)
{
for ( int i = 1; i < word.length(); i++) {
if (word[i] - 'A' < 32
&& word[i - 1] - 'A' >= 32) {
return false ;
}
else if (word[i] - 'A' >= 32
&& word[i - 1] - 'A' < 32) {
if (i - 1 == 0)
continue ;
return false ;
}
}
return true ;
}
int main()
{
string S = "GeeKs" ;
cout << (detectUppercaseUse(S) ? "Yes" : "No" );
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean detectUppercaseUse( char []word)
{
for ( int i = 1 ; i < word.length; i++) {
if (word[i] - 'A' < 32
&& word[i - 1 ] - 'A' >= 32 ) {
return false ;
}
else if (word[i] - 'A' >= 32
&& word[i - 1 ] - 'A' < 32 ) {
if (i - 1 == 0 )
continue ;
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String S = "GeeKs" ;
System.out.print(detectUppercaseUse(S.toCharArray()) ? "Yes" : "No" );
}
}
|
Python3
def detectUppercaseUse(word):
for i in range ( 1 , len (word)):
if ( ord (word[i]) - ord ( 'A' ) < 32 and ord (word[i - 1 ]) - ord ( 'A' ) > = 32 ):
return False ;
elif ( ord (word[i]) - ord ( 'A' ) > = 32 and ord (word[i - 1 ]) - ord ( 'A' ) < 32 ):
if (i - 1 = = 0 ):
continue ;
return False ;
return True ;
S = "GeeKs" ;
print ( "Yes" if detectUppercaseUse(S) else "No" );
|
C#
using System;
class GFG
{
static bool detectUppercaseUse( string word)
{
for ( int i = 1; i < word.Length; i++) {
if (word[i] - 'A' < 32
&& word[i - 1] - 'A' >= 32) {
return false ;
}
else if (word[i] - 'A' >= 32
&& word[i - 1] - 'A' < 32) {
if (i - 1 == 0)
continue ;
return false ;
}
}
return true ;
}
public static void Main()
{
string S = "GeeKs" ;
Console.Write((detectUppercaseUse(S) ? "Yes" : "No" ));
}
}
|
Javascript
<script>
function detectUppercaseUse(word)
{
for (let i = 1; i < word.length; i++) {
if (word[i].charCodeAt(0) - 'A' .charCodeAt(0) < 32
&& word[i - 1].charCodeAt(0) - 'A' .charCodeAt(0) >= 32) {
return false ;
}
else if (word[i].charCodeAt(0) - 'A' .charCodeAt(0) >= 32
&& word[i - 1].charCodeAt(0) - 'A' .charCodeAt(0) < 32) {
if (i - 1 == 0)
continue ;
return false ;
}
}
return true ;
}
let S = "GeeKs" ;
document.write(detectUppercaseUse(S) ? "Yes" : "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...