描述:判断一个字符串是否是首字母大写且非首字母小写。
输入:
一个任意字符串,长度不超过128个字符
输出:
如果输入字符串首字符为大写字母且其他字符非大写字母,返回true
其他情况(空字符串、首字符非字母、首字母为小写、首字母大写但其他字符非字母等)均返回false
样例输入:
Hello world
样例输出:
true
#include <cstdio>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
bool tell( string& line );
int main( void )
{
string line;
getline( cin, line );
if( line.empty() ){
cout << "false" << endl;
return 0;
}
if( tell( line ) )
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
bool tell( string& line )
{
if( !isupper( line[0] ) )
return false;
for( string::size_type i = 1; i < line.size(); ++i ){
if( line[i] == ' ' )
continue;
if( !islower( line[i] ) )
return false;
}
return true;
}
本文介绍了一个算法,用于判断输入字符串是否首字母大写且其他字符非大写,适用于字符串验证场景。
1万+

被折叠的 条评论
为什么被折叠?



