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

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

问题陈述

我们给出了一个字符串‘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 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 using namespace std;bool isValidUpperCase(string str){   for (int i = 1; i = 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) {         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),因为它没有使用任何额外的空间。

结论

在本教程中,用户学会了检查字符串中是否包含有效的大写字符。我们学习了两种不同的方法。在第一种方法中,我们将问题分解为三个部分,在第二种方法中,我们检查相邻元素的字符大小写。然而,这两种代码的时间和空间复杂度相似,但第二种方法的代码更易读。

以上就是检查字符串中的大写字符是否被正确使用的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1443953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 20:48:22
下一篇 2025年12月17日 20:48:30

发表回复

登录后才能评论
关注微信