题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
/** * * @param s string字符串 * @param n int整型 * @return string字符串 */ void convertion(char *s, int n) //将字符串的大小写进行转换 { int i = 0; int j = 0; for (i = 0, j = n - 1; i < j; i++, j--) { if ((s[i] <= 'Z') && (s[i] >= 'A')) { s[i] += 32; } else if ((s[i] <= 'z') && (s[i] >= 'a')) { s[i] -= 32; } if ((s[j] <= 'z') && (s[j] >= 'a')) { s[j] -= 32; } else if ((s[j] <= 'Z') && (s[j] >= 'A')) { s[j] += 32; } } if (n % 2 != 0) { if ((s[i] <= 'Z') && (s[i] >= 'A')) { s[i] += 32; } else if ((s[i] <= 'z') && (s[i] >= 'a')) { s[i] -= 32; } } } char* trans(char* s, int n ) { // write code here char a[10000000] = {0}; int j = 0; convertion(s, n); //将字符串的大小写进行转换 for (int i = n - 1; i >= 0; i--) { if (s[i] == ' ') { strcpy(&a[j], &s[i+1]); a[n-i-1] = ' '; j = n-i; } if (i == 0) { strcpy(&a[j], &s[i]); } } a[n] = '\0'; strcpy(s, a); return s; }