纪念逝去的岁月——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 ...
随机推荐
- 【leetcode】plus One
问题描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...
- Java Security: Illegal key size or default parameters?
来自:http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters I ...
- usb设备驱动描述,王明学learn
usb设备驱动 本章主要内容包含以下:USB总线介绍,USB协议分析,USB系统架构 一.USB总线介绍 1.1USB发展史 USB(Universal Serial Bus)通用串行总线,是一种外部 ...
- Request和Response对象
Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...
- AndroidStudio里面怎么取消与SVN的关联
在公司做项目 遇到SVN解除关联的问题 后经过解决: 1.解除文件的关联方法: 1.1. 创建一个reg文件 如下 1.2 在文件中填入如下内容并保存: Windows Registry Editor ...
- Linux学习笔记(18) Shell编程之流程控制
1. if语句 (1) 单分支if条件语句 格式为: # 注意条件判断式两端的空格if [ 条件判断式 ];then 程序员 fi 或者 if[ 条件判断式 ] then 程序 fi 例:判断分区使用 ...
- 【JAVA】数字相加
设计思想:定义双精度变量 i 和 s=0 ,定义另一个变量 n 作为计数,方便之后求平均值.程序里将数据输入 i 中,通过 s 来将各个数据求和,最后除 n 得平均值. 程序流程图: 源程序代码: p ...
- SpringJMS解析1-使用示例
Spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- SPOJ DISUBSTR 后缀数组
题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...
- PHP 批量修改图片的名字
<?php // glob() 返回指定目录下的文件名以及目录 $arr = glob("img/*.jpg"); $time = time(); $i = 100001; ...