Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505801
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1172
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-21 13:40
个人简介

技术改变命运

文章分类

全部博文(184)

文章存档

2020年(16)

2017年(12)

2016年(156)

我的朋友

分类: C/C++

2016-07-01 09:25:59

I am a students->students a am I
做事要动脑筋,要因情况而定。2016-07-23该
基本思路:
(1)单词单个反转
(2)整体反转

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void reverse(char *str,int begin,int end)
  5. {//实现单词反转
  6.     char temp;
  7.     while (begin < end)
  8.     {
  9.         temp = str[begin];
  10.         str[begin++] = str[end];
  11.         str[end--] = temp;
  12.     }
  13. }
  14. void reverseString(char *str)
  15. {
  16.     int length = strlen(str);
  17.     int begin = -1,end = -1;
  18.     int i = 0;    
  19.     for (i = 0;i < length;i++)
  20.     {
  21.         if (begin == -1 && str[i] == ' ')//处理多个空格
  22.             continue;
  23.         if (begin == -1)//确定起始位置
  24.         {
  25.             begin = i;
  26.             continue;
  27.         }
  28.         if (str[i] == ' ')
  29.         {
  30.             end = i-1;

  31.         }
  32.         else if (i == length-1)
  33.         {
  34.             end = i;
  35.         }
  36.         else
  37.         {
  38.             continue;
  39.         }
  40.         reverse(str,begin,end);
  41.         begin =-1;
  42.         end = -1;
  43.     }
  44.     reverse(str,0,length-1);
  45. }
  46. int main()
  47. {
  48.     char str[] = "I am a students.";
  49.     reverseString(str);
  50.     printf("%s\n",str);
  51.     return 0;
  52. }

阅读(1175) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~