首页 | | 管理入口  
 
我的日历
 
最新评论
 
访问统计
 
我找日志
 
获取RRS
我的 Blog:
birdhook 最新的 20 条日志
[燕山月似钩]
[高山流水]
[随笔心情日记]
[读书笔记]
全站 Blog:
全站最新的 20 条日志
 
友情连接
QQQQQQQQQQQQQQQ
 
将字符串循环移动n位
作者:遥遥  发表时间:2006-3-29

如果对谁有用,那去用吧! <BR/>
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

//将字符串循环移位n位
char src[] = {"1234567890-abcdefg"};


int main(int argc, char* argv[])
{

  char ex, tx;
  int i,j;
  int step =0;
  char * srv, *tmp;
  int len = strlen(src);

  if(argc < 2)
    step = 4;
  else {
    step = atoi(argv[1]);
    if(step%len == 0)
      step = 4;
  }
    
  printf("%s\n", src);
  srv = (char *) malloc(step);
  tmp = (char *) malloc(step);

  strncpy(srv, src, step);

  for(i = 1; i<= len/step ; i++){
    for(j = 0; j < step; j++)
      tmp[j] = src[(i*step+j)%len];
    for(j = 0; j<step; j++)
      src[(i*step+j)%len] = srv[j];
    strncpy(srv, tmp, step);
  }

  for(j=0; j<len%step; j++)
    src[(i*step+j)%len] = srv[j];

  printf("%s\n", src);

  delete srv;
  delete tmp;

  return 0;
}

所属栏目:读书笔记  


 

全部分类中有 1 篇日志 | 每页显示 1 篇


{CommentAuthor}:
{CommentContent}

--- {CommentTime} | {CommentEmail} {CommentUrl} {CommentIp}

Design by smallfish
Powered by 5DBlog.com