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

字符串右移N位题目

时间:2015-01-10 15:17:36      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

  当初想了半天没想出来。。(脑子太笨了。。。。T.T)

回家仔细考虑了下。 实现如下:

void
move_string(char *msg, int steps)
{
  int len;
  int pos;
  int head;
  char tmp;
  int count;
  
  if(msg == NULL)return;
  len = strlen(msg);
  steps = steps % len;
  head = 0;
  count = 0;

  while(1){
    tmp = msg[head];

    if(pos + steps >= len){
      count++;
    }
    
    pos = (pos + steps) % len;
    // back to head do next loop.
    if(pos == head){
      if(count == steps){
	break;
      }
      head++;
      pos = head;
      continue;
    }
    msg[head] = msg[pos];
    msg[pos] = tmp;
  }

  printf("the result : %s\n", msg);
}


字符串右移N位题目

标签:

原文地址:http://my.oschina.net/hjxc/blog/365755

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