随笔-27  评论-6  文章-0  trackbacks-0
#include <stdlib.h>
#include 
<stdio.h>
#include 
<string.h>

//向右循环移位
void loopmover ( char * pstr, int steps )
{
    
int n = strlen(pstr) - steps;
    
char *buff=(char*)malloc(strlen(pstr)+1);    
    strcpy(buff,pstr
+n);
    strncat(buff,pstr,n);
    buff[strlen(pstr)]
='\0';
    strcpy(pstr,buff);    
    free(buff);
}

//向左循环移位
void loopmovel ( char * pstr, int steps )
{
    
char *buff=(char*)malloc(strlen(pstr)+1);    
    strcpy(buff,pstr
+steps);
    strncat(buff,pstr,steps);
    buff[strlen(pstr)]
='\0';
    strcpy(pstr, buff);    
    free(buff);
}

int main ()
{
    
char a[]="abcdefg123456";
    printf(
"%s\n",a);
    loopmover(a,
5);//右移5位
        printf(
"%s\n",a);
    loopmovel(a,
5);
        printf(
"%s\n",a);
    system(
"PAUSE");
    
return 0;
}

运行结果:


C库函数中字符串处理函数集合:

http://www.cppblog.com/GUO/archive/2010/09/13/126522.html
posted on 2010-09-13 19:40 CrazyNerd 阅读(872) 评论(0)  编辑 收藏 引用 所属分类: 数据结构与算法

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理