纪念逝去的岁月——C/C++字符串旋转
几年前,我还不会写这个
例如:
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++字符串旋转的更多相关文章
- 纪念逝去的岁月——C/C++字符串回文
判断字符串是否是回文: 1. 输入:hello world dlrow olleh 输出:1 2. 输入:nihao hello 输出:0 代码 #include <stdio.h> #i ...
- 纪念逝去的岁月——C/C++字符串反转
几年前,我还不会写这个 输入:hello world 输出:dlrow olleh 代码 #include <stdio.h> #include <string.h> void ...
- 纪念逝去的岁月——C++实现一个队列(使用类模板)
1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...
- 纪念逝去的岁月——C++实现一个栈(使用类模板)
这个版本是上个版本的加强版,上个版本的代码:http://www.cnblogs.com/fengbohello/p/4542912.html 目录 1.代码 2.运行结果 1.代码 1.1 调试信息 ...
- 纪念逝去的岁月——C++实现一个栈
1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...
- 纪念逝去的岁月——C/C++排序二叉树
1.代码 2.运行结果 3.分析 1.代码 #include <stdio.h> #include <stdlib.h> typedef struct _Node { int ...
- 纪念逝去的岁月——C/C++二分查找
代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...
- 纪念逝去的岁月——C/C++快速排序
快速排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
- 纪念逝去的岁月——C/C++交换排序
交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...
随机推荐
- 攻城狮在路上(叁)Linux(十八)--- 文件系统的简单操作
本篇仅作为补漏. 一.查看磁盘和目录的容量:df du df:列出文件系统的整体磁盘使用量. du:评估文件系统的磁盘使用量(常用于评估目录所占容量) 二.连接文件:ln 1.hard link:硬 ...
- golang debug with LiteIDE
golang 的调试比较麻烦,debug stop into 无法跳转到自己写的代码,但是能够跳转到系统提供的代码. 以下是简单的测试代码: package main import ( "f ...
- .Net Ioc Unity
Unity 的接口IUnityContainer public interface IUnityContainer : IDisposable IUnityContainer RegisterType ...
- [Oracle] 生产上表的列类型更新
由于粗心,数据库脚本生成的时候错将一个类型NUMBER(5)的字段类型改为 VARCHAR2(5) 直接进行表修改会报错,因为数据已经存在,不能进行更新: ); 大体思路如下: 将要更改类 ...
- MySQL5.7更改密码时出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
转自:http://blog.csdn.net/u010603691/article/details/50379282 新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过 ...
- Android基本认识
AndroidManifest.xml file missing! 是因为开始想当然的用中文当project名 no launcher activity found 第一次运行出了点问题,no lau ...
- cocos2dx游戏开发——别踩白块学习笔记(一)——Block类
一.Block类介绍 当然啦,Block类在这个游戏里就是必需品= =,因为整体都是由这个搞出来的,所以我们可以把游戏需要实现的功能都放在这里. 主要有下面这些功能(经典模式): 1.创建一个Bloc ...
- Java 之 多线程编程
1.线程: a.由来:单任务OS -- 多任务OS b.进程:每一个进程对应一个应用程序,分配独立内存空间 c.线程:线程是进程内部的一个独立的执行分支 d.特点:共享内容地址空间,切换成本更低 2. ...
- 让Web API支持$format参数的方法
public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web AP ...
- loadrunner选择执行哪个Action
深圳湖北籍软件测试群 275212937