给定一个由大写和小写字母组成的字符串S ,任务是检查给定字符串中的大写字符是否正确使用。大写字符的正确用法如下:
- 字符串中的所有字符都是大写的。例如, “极客” 。
- 没有一个字符是大写的。例如, “极客” 。
- 只有第一个字符是大写的。例如, “极客” 。
例子:
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
处理方法:按照以下步骤解决问题:
- 检查字符串的第一个字符是否为大写。如果发现为真,则迭代剩余的字符。
- 如果所有剩余字符都是大写,则打印“Yes” 。否则,如果剩余的任何字符是大写的,则打印“NO” 。
- 如果第一个字符不是大写,则检查剩余的所有字符是否都是小写。如果发现是真的,则打印“YES” 。否则,打印“NO” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the
// character c is in lowercase or not
bool isLower(char c)
{
return c >= 'a' and c <= 'z';
}
// Function to check if the
// character c is in uppercase or not
bool isUpper(char c)
{
return c >= 'A' and c <= 'Z';
}
// Utility function to check if uppercase
// characters are used correctly or not
bool detectUppercaseUseUtil(string S)
{
// Length of string
int N = S.size();
int i;
// If the first character is in lowercase
if (isLower(S[0]))
{
i = 1;
while (S[i] && isLower(S[i]))
++i;
return i == N ? true : false;
}
// Otherwise
else {
i = 1;
// Check if all characters
// are in uppercase or not
while (S[i] && isUpper(S[i]))
++i;
// If all characters are
// in uppercase
if (i == N)
return true;
else if (i > 1)
return false;
// Check if all characters except
// the first are in lowercase
while (S[i] && isLower(S[i]))
++i;
return i == N ? true : false;
}
}
// Function to check if uppercase
// characters are used correctly or not
void detectUppercaseUse(string S)
{
// Stores whether the use of uppercase
// characters are correct or not
bool check = detectUppercaseUseUtil(S);
// If correct
if (check)
cout << "Yes";
// Otherwise
else
cout << "No";
}
// Driver Code
int main()
{
// Given string
string S = "GeeKs";
// Function call to check if use of
// uppercase characters is correct or not
detectUppercaseUse(S);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to check if the
// character c is in lowercase or not
static boolean isLower(char c)
{
return c >= 'a' && c <= 'z';
}
// Function to check if the
// character c is in uppercase or not
static boolean isUpper(char c)
{
return c >= 'A' && c <= 'Z';
}
// Utility function to check if uppercase
// characters are used correctly or not
static boolean detectUppercaseUseUtil(String S)
{
// Length of string
int N = S.length();
int i;
// If the first character is in lowercase
if (isLower(S.charAt(0))) {
i = 1;
while (i 1)
return false;
// Check if all characters except
// the first are in lowercase
while (i
Python3
# Python3 program for the above approach
# Function to check if the
# character c is in lowercase or not
def isLower(c):
return ord(c) >= ord('a') and ord(c) <= ord('z')
# Function to check if the
# character c is in uppercase or not
def isUpper(c):
return ord(c) >= ord('A') and ord(c) <= ord('Z')
# Utility function to check if uppercase
# characters are used correctly or not
def detectUppercaseUseUtil(S):
# Length of string
N = len(S)
i = 0
# If the first character is in lowercase
if (isLower(S[0])):
i = 1
while (S[i] and isLower(S[i])):
i += 1
return True if (i == N) else False
# Otherwise
else:
i = 1
# Check if all characters
# are in uppercase or not
while (S[i] and isUpper(S[i])):
i += 1
# If all characters are
# in uppercase
if (i == N):
return True
elif (i > 1):
return False
# Check if all characters except
# the first are in lowercase
while (S[i] and isLower(S[i])):
i += 1
return True if (i == N) else False
# Function to check if uppercase
# characters are used correctly or not
def detectUppercaseUse(S):
# Stores whether the use of uppercase
# characters are correct or not
check = detectUppercaseUseUtil(S)
# If correct
if (check):
print("Yes")
# Otherwise
else:
print ("No")
# Driver Code
if __name__ == '__main__':
# Given string
S = "GeeKs"
# Function call to check if use of
# uppercase characters is correct or not
detectUppercaseUse(S)
# This code is contributed by mohit kumar 29.
C#
using System;
public class GFG
{
// Function to check if the
// character c is in lowercase or not
static bool isLower(char c)
{
return c >= 'a' && c <= 'z';
}
// Function to check if the
// character c is in uppercase or not
static bool isUpper(char c)
{
return c >= 'A' && c <= 'Z';
}
// Utility function to check if uppercase
// characters are used correctly or not
static bool detectUppercaseUseUtil(string S)
{
// Length of string
int N = S.Length;
int i;
// If the first character is in lowercase
if (isLower(S[0]))
{
i = 1;
while (i < N && isLower(S[i]))
++i;
return i == N ? true : false;
}
// Otherwise
else {
i = 1;
// Check if all characters
// are in uppercase or not
while (i < N && isUpper(S[i]))
++i;
// If all characters are
// in uppercase
if (i == N)
return true;
else if (i > 1)
return false;
// Check if all characters except
// the first are in lowercase
while (i < N && isLower(S[i]))
++i;
return i == N ? true : false;
}
}
// Function to check if uppercase
// characters are used correctly or not
static void detectUppercaseUse(string S)
{
// Stores whether the use of uppercase
// characters are correct or not
bool check = detectUppercaseUseUtil(S);
// If correct
if (check)
Console.WriteLine("Yes");
// Otherwise
else
Console.WriteLine("No");
}
// Driver Code
static public void Main ()
{
// Given string
string S = "GeeKs";
// Function call to check if use of
// uppercase characters is correct or not
detectUppercaseUse(S);
}
}
// This code is contributed by avanitrachhadiya2155
Javascript
输出:
No
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live