码迷,mamicode.com
首页 > 其他好文 > 详细

字符及子串的逆转

时间:2016-06-26 11:26:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

输入 i come from tianjin. 输出 tianjin. from come i

 

int main()
{

  int i = 0, j = 0, flag = 0, begin, end;
  char str[] = "i come from tianjin.";
  char temp;
  j = strlen(str) - 1;
  printf("%s\n", str);
  while (j > i)//逆序整个子串
  {
  temp = str[i];
  str[i] = str[j];
  str[j] = temp;
  j--;
  i++;
  }
  printf("%s\n", str);
  i = 0;
  while (str[i])
  {
  if (str[i] != ‘ ‘)
  {
  begin = i;
  while (str[i] && str[i] != ‘ ‘)
  {
  i++;

  }
  i = i - 1;//这的处理很重要
  end = i;
  }
  cout << begin << " "<<end<<endl;
  while (end > begin)//逆序单词
  {
  temp = str[end];
  str[end] = str[begin];
  str[begin] = temp;
  begin++;
  end--;
  }
  i+=1;//i在递增
}
printf("%s\n", str);
cin.get();
return 0;

}

字符及子串的逆转

标签:

原文地址:http://www.cnblogs.com/ranranblog/p/5617456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!