//将字符串反转,但单词不倒置。 #include<stdio.h> #include<string.h> void reverse(char *s) { char data[255][255];//将s中的空格和非空格子串进行存储 int row = 0,column = 0; int i,j,k; for(i=0;s[i];) { data[row][column] = s[i]; column++; //找到了空格子串的开头 if(s[i] == ‘ ‘) { i++; if(s[i] == ‘ ‘) { do { data[row][column++] = s[i++]; }while(s[i] == ‘ ‘); data[row++][column] = ‘\0‘; column = 0; } else { data[row++][column] = ‘\0‘; column = 0; } } //找到了非空格子串的开头 else if(s[i] != ‘ ‘) { i++; if(s[i] != ‘ ‘) { do { data[row][column++] = s[i++]; }while(s[i] != ‘ ‘ && s[i]);//注意非空格字符还包括字符串结束符。 data[row++][column] = ‘\0‘; column = 0; } else { data[row++][column] = ‘\0‘; column = 0; } } } //将data[][]里面的子串存放于s中。 k = 0; for(i=row-1;i>=0;i--) { for(j=0;data[i][j]!=‘\0‘;j++) { s[k++] = data[i][j]; } } s[k] = ‘\0‘; } int main() { char s[255]; gets(s); reverse(s); puts(s); }
将字符串反转,但单词不倒置。Right here waiting for you! -> you! for waiting here Right
时间: 2024-12-30 00:08:03