#include <algorithm>
#include <cctype>
#include <string>
#include <sstream>
using namespace std;
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param n int整型
* @return string字符串
*/
string trans(string s, int n) {
// write code here
// 这个是所有字符都反转;
reverse(s.begin(), s.end());
// 针对单个字符串进行反转,并修改大小写
for(int i=0; i<=n; ++i)
{
// 遇到非空格
if(s[i]!=' ')
{
if(s.find_first_of(' ',i)!=string::npos)
{
int index = s.find_first_of(' ',i);
int l=i, r=index-1;
while(l<=r)
{
if(islower(s[l]))
s[l] = toupper(s[l]);
else
s[l] = tolower(s[l]);
//对于长度为奇数的字符串,中间字符为l==r,此时如果不加 l!=r 的判断,则该中间字符大小写不变;
if(l!=r)
{
if(islower(s[r]))
s[r] = toupper(s[r]);
else
s[r] = tolower(s[r]);
}
// 调换
swap(s[l],s[r]);
++l;
--r;
}
i=index;
}
else
{
int l=i, r=n-1;
while(l<=r)
{
if(islower(s[l]))
s[l] = toupper(s[l]);
else
s[l] = tolower(s[l]);
if(l!=r)
{
if(islower(s[r]))
s[r] = toupper(s[r]);
else
s[r] = tolower(s[r]);
}
// 调换
swap(s[l],s[r]);
++l;
--r;
}
break;
}
}
}
return s;
// 利用字符串输入流的方法,对空格处理比较麻烦,算了
// string str = "";
// istringstream iss(s);
// string t_str;
// while(iss>>t_str)
// str = t_str + " " + str ;
// // 删除最后一个空格
// str.erase(str.end()-1);
// return str;
}
};

京公网安备 11010502036488号