题解 | #牛群消息传递#
牛群消息传递
https://www.nowcoder.com/practice/28df6c40150a40b49c9c4d4ae1dd675d
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ #include <ctype.h> #include <stdio.h> #include <string.h> void char_reverse(char*str,int left,int right) { while(left<right) { char tmp=str[left]; str[left++]=str[right]; str[right--]=tmp; } } char* reverseWords(char* s ) { // write code here int i=0, j=0; while(s[j]) { while(s[j] && ' ' == s[j]) j++; while(isalpha(s[j])) s[i++] = s[j++]; s[i++] = s[j]; } if(' ' == s[i-2]) s[i-2] = '\0'; char_reverse(s,0, strlen(s)-1); puts(s); for(i=0;s[i];i++) { if(isalpha(s[i])) { int right=i; while(isalpha(s[right])) right++; char_reverse(s,i,right-1); i=right-1; } } return s; }