题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
char* trans(char* ms, int n ) { /*以下为将ms进行大小写翻转后存入s*/ char* str = malloc(n); char* s = calloc(0, n+2); *s='\0'; s++; *s=' '; s++; strcpy(s,ms); memset(str, 0, n + 1); int m = 'a' - 'A'; while (*s != '\0') { if (*s >= 'a' && *s != ' ') { *s = *s - m; } else if (*s != ' ') { *s += m; } s++; } /*以下为s从后往前按照空格进行拆分后填入str中*/ s--; int i = 0; while (*s != '\0') { while (*s != ' ') { i++; s--; } s++; strncpy(str, s, i); strcat(str, " "); str += i + 1; s -= 2; i = 0; } str--; *str='\0'; str -= n; return str; }