几年前,我还不会写这个

例如:

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. Auto Layout

    Auto Layout XCode5+ Auto Layout Concepts 核心的概念是约束. Constraint Basics Constant value Relation Priorit ...

  2. php同步mysql两个数据库中表的数据

    分别创建两个数据库和两张表study库-zone表teaching库-area表 //****SQL脚本****// 1.创建teaching数据库area数据表 create database te ...

  3. phpcms v9 常用调用标签(全)

    本文介绍phpcms v9中模板标签使用说明. {template ) {==}   {/,,)}     loop是data的时候用{thumb($v[thumb],,)} 分页标签------{$ ...

  4. 1.CoreLocation 使用,获取当前位置

    1. ios7只要开始定位,系统就会自动要求你对应用程序授权 ios8之后,必须要代码中实现要求用户对应用程序授权 ,在plist中添加以下两个属性 NSLocationWhenInUseDescri ...

  5. VC 快速创建多层文件夹

    BOOL CreateDirectory( LPCTSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes ); 这个是大多数用户都知道的 ...

  6. OpenGL的消隐与双缓冲

    首先是大家可能已经发现,在我们之前提到的所有例子中,在图形的旋转过程中整个图形都有一定程度的闪烁现象,显得图形的过渡极不平滑,这当然不是我们所要的效果,幸好opengl 支 持一个称为双缓存的技术,可 ...

  7. C# 生成随机数

    private static char[] constant = { ', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p ...

  8. BootSrap学习

    1.当row1里边有2个或多个col的时候,如果位置在后边的col的高度大于位置在前边的col,就可能会导致下一个row2在满足col-xs-*在满足和为12的情况下错乱分行.解决办法是强行给row1 ...

  9. Spring Data JPA 查询方法支持的关键字

    Table 2.3. Supported keywords inside method names Keyword Sample JPQL snippet And findByLastnameAndF ...

  10. MFC 修改 单文档 SDI 窗体 标题

    程序主窗口标题栏的一般形式为: 文档标题—主窗口标题 1.修改文档标题 修改CExamDoc::onNewDocument()函数为: BOOL CExamDoc::OnNewDocument() { ...