几年前,我还不会写这个

例如:

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. android 入门-Service

    sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...

  2. phpcms_v9 多图字段 内容页,首页,分页自定义字段调用

    phpcms_v9 多图字段 内容页,首页,分页自定义字段调用 说明:自定义多图字段名 shigongtu 1 内容页调用 {loop $shigongtu $r}      <img src= ...

  3. OK6410移植madplay播放器,王明学learn

    对于ok6410的madplay移植主要包括三部分.声卡驱动移植,播放器的移植,以及alsa库的移植. 一.首先移植声卡驱动以及播放器 ok6410采用WM97系列的声卡芯片,要使得内核支持该驱动,首 ...

  4. C语言字符串长度(转)

    C语言字符串长度的计算是编程时常用到的,也是求职时必考的一项. C语言本身不限制字符串的长度,因而程序必须扫描完整个字符串后才能确定字符串的长度. 在程序里,一般会用strlen()函数或sizeof ...

  5. LeetCode——Reverse Integer(逆置一个整数)

    问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Ha ...

  6. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  7. BPEL是个什么东东

    研究团队有个做智能服务组合的,其中用到叫BPEL的东西,因为全称是Business Process Execution Language,译成中文就是商业执行过程语言,这个东东的是整合SOA的一个执行 ...

  8. Element selector doesn't have required

    这个错误是因为创建xml文件时文件类型弄成了layout xml file ,这样就会自动到layout文件夹下 应该是drawable resource file

  9. 深入理解KMP算法之续篇

    前言: 纠结于KMP已经两天了,相较于本人之前博客中提到的几篇博文,本人感觉这篇文章更清楚地说明了KMP算法的来龙去脉. http://www.cnblogs.com/goagent/archive/ ...

  10. Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器

    1.前提是:windows已经配置好Python的环境变量: 2.进入cmd命令行模式: **输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题 **用cd命令进入Python脚 ...