#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
// 输入整数n
cin>>n;
cin.ignore();
string arr[n];
// 循环读取n行字符串
for (int i = 0; i<n ;i++)
{
getline(cin,arr[i]);
}
for (int i = 0; i<n ;i++){
for(int j=0;j<arr[i].size();j++){
char a=arr[i][j];
if(j==0 and a>=97 and a<=122){
a-=32; // 将首字母小写字母转换为大写字母
}else if(j!=0 and a>=65 and a<=90){
a+=32; // 将非首字母大写字母转换为小写字母
}
cout<<a; // 输出转换后的字符
}
cout<<endl; // 换行
}
return 0;
}
该段代码实现了输入n行字符串,对每行字符串进行大小写转换的功能。首先,通过cin>>n
语句输入一个整数n,表示接下来要输入的字符串行数。接着,通过cin.ignore()
语句忽略掉之前输入n的行末的换行符。然后,定义一个大小为n的字符串数组arr用于存储输入的n行字符串。使用getline(cin,arr[i])
循环读取n行字符串。接下来,通过两层循环遍历每行字符串的每个字符。如果当前字符是该行字符串的首字母且为小写字母,则将其转换为大写字母;如果当前字符不是首字母且为大写字母,则将其转换为小写字母。最后,输出转换后的字符,并在每行字符串结束后换行。最后,程序返回0,表示正常结束。