问题131A-http://codeforces.com/problemset/problem/131/A我的解决方案不是按照问题的要求进行,而是返回输入的相同字符串。我的解决方案出了什么问题。
我的解决方案:
#include<iostream>
#include<cstring>
using namespace std;
int i,a;
char input[101];
int main()
{
cin>>input;
a=0;
for(i=0;input[i]!=NULL;i++) //To find number of uppercase letters.
{
if(isupper(input[i]))
a++;
}
if((a==strlen(input))|((a==(strlen(input)-1))&(islower(input[0])))) //To check for accidental pressing of Caps Lock(Words like Jingle or jINGLE.).
{
toupper(input[0]);
for(i=1;input[i]!=NULL;i++)
{
tolower(input[i]);
}
cout<<input;
}
else
cout<<input;
}
toupper()
或tolower()
不更改其参数,而是返回更改后的字符。您必须编写input[i] = toupper(input[i])
。那么就不应该用逐位运算符进行这些条件检查。请改用||
和&&
。
toupper(input[0]);
不将input[0]
更改为大写,它返回相当于input[0]
的大写。
尝试
input[0] = toupper(input[0]);
相反(对于其他所有toupper/tolor调用也是如此)。
char toupper(char)
和char tolower(char)
只返回转换后的字符,不修改传递的字符(需要使用引用或指针)。改为使用:
input[0] = toupper(input[0]);
for(i=1;input[i]!=NULL;i++)
{
input[i] = static_cast<char> ( tolower( static_cast<unsigned char>(input[i]) ) );
}
cout<<input;