【发布时间】:2022-06-16 21:23:49
【问题描述】:
您好,我正在尝试解决“cAPS lOCK”问题,当我输入“cAPS”时,它应该打印“Caps”,但它会打印“caPScapScapscaps”,即使我在以下情况下暂停程序以停止程序条件为真,它继续运行这是我的代码。
#include <iostream>
using namespace std;
int main()
{
// -if all letters are capital:
//all of them become small
//-if all letters EXCEPT the first letters are capital:
//all of them become small except the first one becomes capital
//-everything else:
//keep the word as it is
string s;
cin>>s;
int c=0;
for(int i=0;s[i]!='\0';i++)
{
c++;
}
for(int j=0;j<c;j++)
{
if(s[j]>=65 && s[j]<=90)
{
s[j]=s[j]+32;
cout<<s;
break;
}
for(int k=1;k<c;k++)
{
if(s[k]>=65 && s[k]<=90)
{
s[k]=s[k]+32;
cout<<s;
break;
}
}
}
cout<<s;
}
【问题讨论】:
-
保持不一致的缩进将使不平衡括号的问题更难发现。修复它,错误可能会更加明显。