题目:翻转单词顺序

参考文献:

剑指offer 何海涛老师 P220

1.翻转整个句子

如 I  am alex! ---> !xela am I

void reverseSentence(char *sen,int n){

int start=0;

//!int end1 = sizeof(sen)/sizeof(sen[0])-1;

// !printf("end1=%d\n",end1);

int end = n-1;

//printf("end=%d\n",end);

//不能用start<=n/2。

while(start

//A^0 = A; A^1 = A的各位相反

//A^A=0 A=A^B B=B^A=B^A^B=A; A=A^B=A^B^A=B

sen[start] ^= sen[end];

sen[end] ^= sen[start];

sen[start] ^= sen[end];

start++;

end--;

}

}

2.翻转单词顺序

如I am alex! --> alex! am I 。其实只需要在1的基础上继续将每个单词顺序颠倒。

void reverseWordsInSentence(char *sen){

char *high,*low;

reverseSentence(sen,strlen(sen));

printf("sen转换后:\n");

puts(sen);

high = sen;

low = sen;

printf("word转换后:\n");

while(*high!='\0'){

//找到不是空格的字符即为单词的头。

while(*high==' ' && *high!='\0'){

high++;

}

low = high;

//找到空格的字符即为单词结尾的下一个字符

while(*high!=' ' && *high!='\0'){

high++;

}

//此时交换一个单词的首尾

reverseSentence(low,high-low);

puts(sen);

}

}

3.左旋转字符串。

如abceefg 左旋转2个字符得到ceefgab

void leftReverse(char *sen,int n){

char *high;

int len = n, i=0;

high = sen;

//添加边界值判断。当n==strlen(sen)不需要旋转 。当n<0也不需要旋转。当字符串为空,更不用旋转

if(strlen(sen) >0 && n>0 && n < strlen(sen)){

for(;i

high++;

}

reverseSentence(sen,len);

printf("%c,%d\n",*high,strlen(sen)-len);

reverseSentence(high,strlen(sen)-len);

reverseSentence(sen,strlen(sen));

}

}

下面给出c++实现的版本:

c++是string 操作有一些方法,如求长度 str.length(),对字符串的修改不会反映到字符串中,要保存结果可以用replace() 替换字符。

如string s = "abcdefg";s.replace(2,3,"!!!!!");//从索引2开始3个字节的字符全替换成"!!!!!"

string ReverseSentence(string str) {

int high,low;

str=reverseSentence(str,str.length());

cout<

high = 0;

low = 0;

string temp;

while(str[high]!='\0'){

//找到不是空格的字符即为单词的头。

while(str[high]==' ' && str[high]!='\0'){

high++;

}

low = high;

//找到空格的字符即为单词结尾的下一个字符

while(str[high]!=' ' && str[high]!='\0'){

high++;

}

//此时交换一个单词的首尾

temp=reverseSentence(str.substr(low,str.length()),high-low);

cout<

str.replace(low,str.length(),temp);

}

return str;

}

string reverseSentence(string sen,int n){

int start=0;

//!int end1 = sizeof(sen)/sizeof(sen[0])-1;

// !printf("end1=%d\n",end1);

int end = n-1;

//printf("end=%d\n",end);

//不能用start<=n/2。

while(start

//A^0 = A; A^1 = A的各位相反

//A^A=0 A=A^B B=B^A=B^A^B=A; A=A^B=A^B^A=B

sen[start] ^= sen[end];

sen[end] ^= sen[start];

sen[start] ^= sen[end];

start++;

end--;

}

return sen;

}

string leftReverse(string str, int n) {

int high;

int len = str.length(), i=0;

high = 0;

string temp,temp2;

//添加边界值判断。当n==strlen(sen)不需要旋转 。当n<0也不需要旋转。当字符串为空,更不用旋转

if(len>0 && n>0 && n < len){

for(;i

high++;

}

temp=reverseSentence(str,n);

cout<

str.replace(0,temp.length(),temp);

temp2=reverseSentence(str.substr(high,len),len-n);

str.replace(high,len,temp2);

str=reverseSentence(str,len);

}

return str;

}

翻转单词顺序列C语言,剑指offer刷题之c、c++实现的翻转单词顺序列相关推荐

  1. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  2. 【LeetCode 剑指offer刷题】查找与排序题14:Wiggle Sort(系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Wiggle Sort II Given an unsorted array nums, reorder it su ...

  3. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  4. 【LeetCode 剑指offer刷题】字符串题6:67 把字符串转成整数

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 67 把字符串转成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符 ...

  5. 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...

  6. 【LeetCode 剑指offer刷题】回溯法与暴力枚举法题6:Number of Islands

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Number of Islands Given a 2d grid map of '1's (land) and ' ...

  7. 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...

  8. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  9. 【LeetCode 剑指offer刷题】树题19:8 二叉树中序遍历的下一个结点

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 8 二叉树中序遍历的下一个结点 题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注 ...

最新文章

  1. java对象生命周期_Java对象生命周期和类生命周期
  2. Ollydbg使用教程学习总结(五)
  3. Android 应用开发(第四章)---APP数据埋点
  4. const 与 readonle 的异同
  5. 基于分割的PTD渐进三角网加密滤波(SBF)算法
  6. java提高篇(十三)-----字符串
  7. 跑路了,在国外当程序员有多爽?
  8. lingo3d_基于官方教程的分析
  9. Arm Compiler 5 在 Keil MDK 5.37中不可用
  10. 看我如何自制安全的远程控制工具
  11. MySQL窗口函数OVER()
  12. linux下qt打印功能如何实现,Qt Graphics-View的打印功能实现
  13. CleanCode-函数
  14. Swing交通罚单管理系统java车辆违章缴费金额查询交警信息jsp源代码Maven数据库mysql
  15. QPushButton禁用状态文字变形变粗
  16. ant man什么意思_ant是什么意思_ant翻译_读音_用法_翻译
  17. 互动式广告是怎么样的一种广告形式?
  18. 博客大赛,我的一场生意一场梦
  19. 成长与危险相伴是常态,加强安全审计才是硬道理
  20. 【python-04】

热门文章

  1. IOS机型margin属性无效问题
  2. 浅谈Spring事务隔离级别
  3. Tomcat根目录下work文件夹的作用
  4. Spark入门实战系列--6.SparkSQL(上)--SparkSQL简介
  5. html template--(来自网易)
  6. 用C做的电子时钟程序
  7. oracle字段规则,Oracle的基本操作+Oracle字段类型(zz)
  8. 用dos复制文件_一文带你熟悉DOS命令操作,CMD从此不再是路人!
  9. 8g ubuntu 树莓派4b_树莓派4B如何安装ubuntu20.04
  10. linux清空输入框,Linux uniq 命令