您好,欢迎访问一九零五行业门户网

检查字符串中的大写字符是否被正确使用

问题陈述我们给出了一个字符串‘str’,包含大写或小写的字母字符。我们需要检查字符串中大写字符的使用是否正确。
以下是在字符串中正确使用大写字母的方法。
如果只有第一个字符是大写的,其他字符都是小写的。
如果字符串的所有字符都是小写。
如果字符串的所有字符都是大写。
示例输入hello
输出valid
explanation的中文翻译为:解释“hello”中,只有第一个字符为大写,其他字符均为小写,因此是一个有效的字符串。
输入'hello'
输出'valid'
explanation的中文翻译为:解释在“hello”字符串中,所有字符都是小写的,因此它是一个有效的字符串。
输入‘hello’
输出‘not valid’
explanation的中文翻译为:解释在字符串‘hello’中,第一个字符是小写的,但是最后3个字符是大写的,所以这个字符串是无效的。
方法一在这种方法中,如果第一个字符是小写字母,我们检查字符串的所有字符是否都是小写字母,并返回一个布尔值。如果第一个字符是大写字母,我们检查所有其他字符是大写字母或小写字母,并返回布尔值。
算法步骤 1 - 定义 islower() 函数,该函数将单个字符作为参数,并返回布尔值该字符是否为小写。如果‘character-a’大于或等于32,则表示字符为小写。
步骤 2 - 定义 isupper() 函数,就像 islower() 函数一样,并根据字符是否为大写字母返回一个布尔值。
第 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 notbool islower(char c){ return c - 'a' >= 32;}// check if character c is in uppercase or notbool 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 validtutorialspoint : validtutorialspoint : validtutorialspoint : valid

时间复杂度 − o(n),因为它需要使用循环遍历字符串。islower()和isupper()函数的时间复杂度为o(1)。
空间复杂度 − o(1),因为它没有使用任何额外的空间。
方法2在下面的方法中,我们优化了第一种方法的代码。在这里,我们检查字符串中除前两个字符之外的两个相邻元素是否大小写相同,以检查字符串是否包含有效的大写字符。
算法步骤 1 − 使用 for 循环从字符串的第一个索引迭代到最后一个索引。
第二步 - 在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 validtutorialspoint : validtutorialspoint : validtutorialspoint : valid

时间复杂度 - o(n),因为它需要使用循环遍历字符串。
空间复杂度 − o(1),因为它没有使用任何额外的空间。
结论在本教程中,用户学会了检查字符串中是否包含有效的大写字符。我们学习了两种不同的方法。在第一种方法中,我们将问题分解为三个部分,在第二种方法中,我们检查相邻元素的字符大小写。然而,这两种代码的时间和空间复杂度相似,但第二种方法的代码更易读。
以上就是检查字符串中的大写字符是否被正确使用的详细内容。
其它类似信息

推荐信息