几年前,我还不会写这个

例如:

1、向右→旋转5个字符

输入:HelloWorld

输出:WorldHello

2、向右→旋转3个字符

输入:HelloWorld

输出:rldHelloWo

代码

 #include <string.h>
#include <stdio.h>
#include <stdlib.h> int scrollstr(char * p, int iStep)
{
if(NULL == p)
{
return -;
}
int iLen = strlen(p);
iStep %= iLen;
if( == iStep)
{
return ;
}
char * pt = (char *)malloc(iLen + );
if(NULL == pt)
{
return -;
}
memset(pt, , iLen + );
int i = ;
for(i = ; i <= iStep; i++)
{
pt[iStep - i] = p[iLen - i];
}
for(i = ; i <= iLen - iStep; i++)
{
p[iLen - i] = p[iLen - i - iStep];
}
for(i = ; i < iStep; i++)
{
p[i] = pt[i];
} return ;
} int main()
{
char pX[] = {"HelloWorld"}; printf("src : [%s]\n", pX);
scrollstr(pX, );
printf("dst : [%s]\n", pX); return ;
}

编译

$ g++ -o scrollstring scrollstring.cpp

运行

$ ./scrollstring
src : [HelloWorld]
dst : [WorldHello]

再见……

纪念逝去的岁月——C/C++字符串旋转的更多相关文章

  1. 纪念逝去的岁月——C/C++字符串回文

    判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...

  2. 纪念逝去的岁月——C/C++字符串反转

    几年前,我还不会写这个 输入:hello world 输出:dlrow olleh 代码 #include <stdio.h> #include <string.h> void ...

  3. 纪念逝去的岁月——C++实现一个队列(使用类模板)

    1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...

  4. 纪念逝去的岁月——C++实现一个栈(使用类模板)

    这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...

  5. 纪念逝去的岁月——C++实现一个栈

    1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...

  6. 纪念逝去的岁月——C/C++排序二叉树

    1.代码 2.运行结果 3.分析 1.代码 #include <stdio.h> #include <stdlib.h> typedef struct _Node { int ...

  7. 纪念逝去的岁月——C/C++二分查找

    代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...

  8. 纪念逝去的岁月——C/C++快速排序

    快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  9. 纪念逝去的岁月——C/C++交换排序

    交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

随机推荐

  1. .NET NLog 详解(五) - Condition Expression

    Sample <!-- during normal execution only log Info messages --> <defaultFilter>level > ...

  2. hibernate base

    第一个类:Person.java package org.crazyit.app.domain; import java.io.Serializable;import java.util.ArrayL ...

  3. android:layout_weight属性详解 (转)

    在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...

  4. Laravel之Service Container服务容器

    managing class dependencies and performing dependency injection. Dependency injection is a fancy phr ...

  5. javascript同名变量

    我写个流程:在流程之前,必须写一下标识符是啥. 一句话,就是variable object的属性.而这个对象会被不同执行环境来决定. 比如全局环境下的variable object 就是 global ...

  6. Codeforces Round #354 (Div. 2)-D

    D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...

  7. css随记02布局

    布局 二栏布局 利用absolute, margin .container { position: relative; } nav { position: absolute; left: 0px; w ...

  8. localhost和127.0.0.1 的区别

  9. hdu 1520 Anniversary party 基础树dp

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  10. 2.2 代码块--delphi 写日志模块

    //2.2 代码块--写日志 //调用例句如:LogMsg('FTP上传线程终止',False,true); procedure LogMsg(AMsg: string; const blnIsErr ...