问题:
将一句话里的单词进行倒置,标点符号不倒置。分析:
1) 将字符串完全倒置.
2)将每个单词进行部分倒置.
代码:
//单词倒置 #include <iostream> #include <stdio.h> #include <string> using namespace std; int main ( ) { int j =0; int i =0; int begin,end; char temp; char str[ ] = "Welcome to BeiJing"; //string str; //cin>>str; j=strlen(str)-1; //j=str.length()-1; printf ("显示输入字符串String:\n\t%s\n",str); //第一步是进行全盘翻转 while(j>i) { // str[j] = temp[i]; temp =str[i]; str[i] =str[j]; str[j] =temp; j--; i++; } printf ("显示字符串全部倒置String:\n\t%s\n",str); i=0; //第二步进行部分翻转,如果不是空格,则开始翻转为单词 while(str[i]) { //str[j] = temp[i]; if(str[i]!=' ') { begin =i; while(str[i]&&str[i] !=' ') { i++; } i =i-1; end =i; } while(end >begin) { temp =str[begin]; str[begin] = str[end]; str[end] =temp; end--; begin++; } i++; } printf ("显示字符串单词倒置String:\n\t%s\n",str); return 0; }
输出结果:
转载请注明出处:http://blog.csdn.net/utimes/article/details/8453922