题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param n int整型 * @return string字符串 */ char* trans(char* s, int n ) { for (int i = 0; i < n; i++) { if (s[i] - 'A' < 32 && s[i] != ' ') { s[i] = s[i] + 32; } else if (s[i] - 'A' >= 32 && s[i] != ' ') { s[i] = s[i] - 32; } } // write code here for (int i = 0; i < n / 2; i++) { char tem = s[i]; s[i] = s[n - i - 1]; s[n - i - 1] = tem; } // 反转每个单词 char* start=s; for(int i=0;i<=n;i++){ if(s[i]==' '|| s[i]=='\0'){ int len=s+i-start; for(int j=0;j<len/2;j++){ start[j]=start[j]^start[len-j-1]; start[len-j-1]=start[j]^start[len-j-1]; start[j]=start[j]^start[len-j-1]; } start=s+i+1; } } return s; }