题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param n int整型
* @return string字符串
*/
// 从末端往前寻找空格,作为分割点,然后将那一串字母进行大小写转换
// 大小写字母转换完全后,判断空格,接着寻找下一处字母
char* trans(char* s, int n ) {
// write code here
char dest[10000000];
int start, end, j, i, tmp;
end = n - 1;
start = n;
j = 0;
for(i = n - 1; i >= 0; i--)
{
if(s[i] == ' ')
{
start = i + 1;
}
else if(i == 0)
start = 0;
tmp = start;
// 用start 和 end 作为字母的分割线
for(;end >= start; end--)
{
if(s[tmp] >= 'a' && s[tmp] <= 'z')
{
dest[j] = s[tmp] + ('A' - 'a');
}
else if(s[tmp] >= 'A' && s[tmp] <= 'Z')
{
dest[j] = s[tmp] + ('a' - 'A');
}
j++;
tmp++;
}
// 遇到空格
if(s[end] == ' ')
{
dest[j] = ' ';
j++;
end--;
}
}
return dest;
}

查看17道真题和解析