Check if uppercase characters in a string are used correctly or not
Last Updated :
28 May, 2021
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: Follow the steps below to solve the problem:
- Check if the first character of the string is in uppercase or not. If found to be true, iterate over the remaining characters.
- If all the remaining characters are in uppercase, print “Yes”. Otherwise, if any of the remaining characters are in uppercase, then print “NO”.
- If the first character is not in uppercase, then check if all the remaining characters are in lowercase or not. If found to be true, then print “YES”. Otherwise, print “NO”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isLower( char c)
{
return c >= 'a' and c <= 'z' ;
}
bool isUpper( char c)
{
return c >= 'A' and c <= 'Z' ;
}
bool detectUppercaseUseUtil(string S)
{
int N = S.size();
int i;
if (isLower(S[0]))
{
i = 1;
while (S[i] && isLower(S[i]))
++i;
return i == N ? true : false ;
}
else {
i = 1;
while (S[i] && isUpper(S[i]))
++i;
if (i == N)
return true ;
else if (i > 1)
return false ;
while (S[i] && isLower(S[i]))
++i;
return i == N ? true : false ;
}
}
void detectUppercaseUse(string S)
{
bool check = detectUppercaseUseUtil(S);
if (check)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
string S = "GeeKs" ;
detectUppercaseUse(S);
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isLower( char c)
{
return c >= 'a' && c <= 'z' ;
}
static boolean isUpper( char c)
{
return c >= 'A' && c <= 'Z' ;
}
static boolean detectUppercaseUseUtil(String S)
{
int N = S.length();
int i;
if (isLower(S.charAt( 0 ))) {
i = 1 ;
while (i<N && isLower(S.charAt(i)))
++i;
return i == N ? true : false ;
}
else {
i = 1 ;
while (i<N && isUpper(S.charAt(i)))
++i;
if (i == N)
return true ;
else if (i > 1 )
return false ;
while (i<N && isLower(S.charAt(i)))
++i;
return i == N ? true : false ;
}
}
static void detectUppercaseUse(String S)
{
boolean check = detectUppercaseUseUtil(S);
if (check)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args)
{
String S = "GeeKs" ;
detectUppercaseUse(S);
}
}
|
Python3
def isLower(c):
return ord (c) > = ord ( 'a' ) and ord (c) < = ord ( 'z' )
def isUpper(c):
return ord (c) > = ord ( 'A' ) and ord (c) < = ord ( 'Z' )
def detectUppercaseUseUtil(S):
N = len (S)
i = 0
if (isLower(S[ 0 ])):
i = 1
while (S[i] and isLower(S[i])):
i + = 1
return True if (i = = N) else False
else :
i = 1
while (S[i] and isUpper(S[i])):
i + = 1
if (i = = N):
return True
elif (i > 1 ):
return False
while (S[i] and isLower(S[i])):
i + = 1
return True if (i = = N) else False
def detectUppercaseUse(S):
check = detectUppercaseUseUtil(S)
if (check):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
S = "GeeKs"
detectUppercaseUse(S)
|
C#
using System;
public class GFG
{
static bool isLower( char c)
{
return c >= 'a' && c <= 'z' ;
}
static bool isUpper( char c)
{
return c >= 'A' && c <= 'Z' ;
}
static bool detectUppercaseUseUtil( string S)
{
int N = S.Length;
int i;
if (isLower(S[0]))
{
i = 1;
while (i < N && isLower(S[i]))
++i;
return i == N ? true : false ;
}
else {
i = 1;
while (i < N && isUpper(S[i]))
++i;
if (i == N)
return true ;
else if (i > 1)
return false ;
while (i < N && isLower(S[i]))
++i;
return i == N ? true : false ;
}
}
static void detectUppercaseUse( string S)
{
bool check = detectUppercaseUseUtil(S);
if (check)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static public void Main ()
{
string S = "GeeKs" ;
detectUppercaseUse(S);
}
}
|
Javascript
<script>
function isLower(str) {
return str === str.toLowerCase();
}
function isUpper(str) {
return str === str.toUpperCase();
}
function detectUppercaseUseUtil(S) {
var N = S.length;
var i;
if (isLower(S[0])) {
i = 1;
while (S[i] && isLower(S[i])) ++i;
return i === N ? true : false ;
}
else {
i = 1;
while (S[i] && isUpper(S[i])) ++i;
if (i === N) return true ;
else if (i > 1) return false ;
while (S[i] && isLower(S[i])) ++i;
return i === N ? true : false ;
}
}
function detectUppercaseUse(S) {
var check = detectUppercaseUseUtil(S);
if (check) document.write( "Yes" );
else document.write( "No" );
}
var S = "GeeKs" ;
detectUppercaseUse(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...