牛客BM83 字符串变形_c++
描述
牛客BM83 字符串变形_开发语言_02
示例1

输入:
"This is a sample",16
返回值:
"SAMPLE A IS tHIS"

示例2

输入:
"nowcoder",8
返回值:
"NOWCODER"

示例3

输入:
"iOS",3
返回值:
"Ios"

主要思路:全部遍历添加到vector
Code:

class Solution {
public:


string trans(string s, int n) {
vector<string>vec;
vector<string>sepvec;
string str;
string sepstr;
string res;
for(int i=0;i<s.length();i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
str+=tolower(s[i]);
if(sepstr.length())
{
sepvec.push_back(sepstr);
sepstr="";
}
}
else if(s[i]>='a'&&s[i]<='z')
{
str+=toupper(s[i]);
if(sepstr.length())
{
sepvec.push_back(sepstr);
sepstr="";
}
}
if(s[i]==' ')
{
sepstr+=' ';
if(str.length())
vec.push_back(str);
str="";
}
}
if(sepstr.size())
{
sepvec.push_back(sepstr);
sepstr="";
}
if(str.length())
{
vec.push_back(str);
str="";
}

int index=sepvec.size()-1;
int index2=vec.size()-1;
if(s[s.length()-1]==' ')
{
while (1) {
if(index>=0)
res+=sepvec[index--];
if(index2>=0)
res+=vec[index2--];
if(index<0&&index2<0)
{
break;
}
}

}
else
{
while (1) {

if(index2>=0)
res+=vec[index2--];
if(index>=0)
res+=sepvec[index--];
if(index<0&&index2)
{
break;
}
}
}

cout<<res<<endl;
return res;
}
};