检查字符串中大写字符的使用是否正确
问题陈述
给定一个字符串 'str',其中包含大写或小写的字母字符。我们需要检查字符串中大写字符的使用是否正确。
以下是字符串中使用大写字符的正确方法。
如果只有第一个字符是大写,其他字符是小写。
如果字符串的所有字符都是小写。
如果字符串的所有字符都是大写。
示例
输入
"Hello"
输出
"valid"
解释
在 "Hello" 中,只有第一个字符是大写,其他字符是小写,所以这是一个有效的字符串。
输入
'hello'
输出
'valid'
解释
在 'hello' 字符串中,所有字符都是小写,所以这是一个有效的字符串。
输入
‘heLLO’
输出
‘Not Valid’
解释
在 'heLLO' 字符串中,第一个字符是小写,但最后三个字符是大写,所以字符串无效。
方法一
在这种方法中,如果第一个字符是小写,我们检查字符串的所有字符是否都是小写并返回布尔值。如果第一个字符是大写,我们检查所有其他字符是大写还是小写,并返回布尔值。
算法
步骤 1 - 定义 isLower() 函数,它接受单个字符作为参数并返回布尔值,指示该字符是否为小写。如果 '字符-A' 大于或等于 32,则表示字符是小写。
步骤 2 - 定义与 isLower() 函数类似的 isUpper() 函数,并根据字符是否为大写返回布尔值。
步骤 3 - 定义 isValidUpper() 函数,该函数检查字符串是否包含所有有效的大写字符。
步骤 4 - 在 isValidUpper() 函数中,使用 isLower() 函数,并检查第一个字符是否为小写。如果是,则使用循环和 isUpper() 函数检查所有其他字符。如果任何字符是大写,则返回 false。否则,如果所有字符都是小写,则返回 true。
步骤 5 - 如果第一个字符是大写,我们需要检查两种情况。首先,所有字符都可以是大写,或者所有字符都可以是小写,除了第一个字符。
步骤 5.1 - 定义 'totalUpper' 变量并将其初始化为 1。
步骤 5.2 - 统计字符串中大写字符的总数。
步骤 5.3 - 如果大写字符的总数等于 1 或字符串的长度,则表示字符串包含有效的大写字符,返回 true。否则,返回 false。
示例
#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;
}
输出
TutorialsPoint : Not valid tutorialspoint : Valid Tutorialspoint : Valid TUTORIALSPOINT : Valid
时间复杂度 - O(N),因为它需要使用循环迭代字符串。isLower() 和 isUpper() 函数的时间复杂度为 O(1)。
空间复杂度 - O(1),因为它没有使用任何额外的空间。
方法二
在下面的方法中,我们优化了第一种方法的代码。在这里,我们除了前两个字符外,检查字符串的两个相邻元素是否处于相同的大小写,以检查字符串是否包含有效的大写字符。
算法
步骤 1 - 使用 for 循环从字符串的第一个索引迭代到最后一个索引。
步骤 2 - 在 for 循环中,如果当前字符是大写,而前一个字符是小写,则返回 false,因为它不是有效的字符串。
步骤 3 - 如果当前字符是小写,而前一个字符是大写,则执行以下步骤。
步骤 3.1 - 检查前一个字符是否位于第 0 个索引或字符串的第一个字符,并在 for 循环中继续。
步骤 3.2 - 如果前一个字符不是第一个字符,则返回 false。
示例
#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;
}
输出
TutorialsPoint : Not valid tutorialspoint : Valid Tutorialspoint : Valid TUTORIALSPOINT : Valid
时间复杂度 - O(N),因为它需要使用循环迭代字符串。
空间复杂度 - O(1),因为它没有使用任何额外的空间。
结论
在本教程中,用户学习了如何检查字符串是否包含有效的大写字符。我们学习了两种不同的方法。在第一种方法中,我们将问题分解为三个部分,在第二种方法中,我们检查相邻元素的字符大小写。然而,两种代码的时间和空间复杂度都相似,但第二种方法的代码更易读。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP