在任何情况下都会出现意外输出.我的解决方案出了什么问题
Unexpected output in every case.What is wrong with my solution?
问题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;
相关文章:
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- C++集<T> 什么是最合适的解决方案?
- 使用STL对用户输入数组进行排序的错误有什么解决方案吗?
- 此解决方案中生成更改的方法数量(自上而下)有什么问题?
- 在这种情况下,我们可以使用静态而不是朋友吗,还有其他解决方案是什么
- 模板重载解决方案:当多个模板匹配时会发生什么?
- 什么是最快的解决方案,用于读/写入哈希映射
- 构建C 插件时的错误,该解决方案是什么
- 我的构建二叉树解决方案有什么问题?
- 从QTcpSocket上的数据流中连续运行复杂算法的最佳Qt线程解决方案是什么
- 正在(在构造函数中)将其包含一个不良设计的指针传递,如果是的,则解决方案是什么
- Qt 创建者,跟踪:不允许操作.什么是永久解决方案
- 暂停和恢复线程的最佳解决方案是什么?
- Windows上的模板有什么问题?解决方案是什么
- 为什么“从'x*'到'y'失去精度”是一个硬错误,什么是适合旧版代码的解决方案
- 在 SPOJ 上解决 JUICE 时,此解决方案有什么问题?
- 简单程序没有输出和崩溃.我的解决方案有什么问题
- 宿醉 spoj,解决方案中有什么问题
- C++构建一个带有运算符重载的双边树-什么是好的内存解决方案