首页 > 代码库 > 将字符串反转,但单词不倒置。Right here waiting for you! -> you! for waiting here Right
将字符串反转,但单词不倒置。Right here waiting for you! -> you! for waiting here Right
//将字符串反转,但单词不倒置。#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);}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。