编程实现字符串的循环移位操作
2013-06-05 21:42
387 查看
/****************************************
编程实现字符串的循环移位操作
函数头是这样的:
pStr是指向以'\0'结尾的字符串的指针
steps是要求移动的n
void LoopMove ( char * pStr, int steps )
{
// 请填充...
}
*****************************************/
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int MAX_LEN=255;
void LoopMove1( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen ( pStr ) ) = '\0';
strcpy( pStr, tmp );
}
void test_LoopMove1()
{
char c[]="future_money_wifi_parent_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove1(c,7);
cout<<"after LoopMove1(c,7) c[] is :"<<c<<endl;
}
void LoopMove2( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
memcpy( tmp, pStr + n, steps );
memcpy(pStr + steps, pStr, n );
memcpy(pStr, tmp, steps );
}
void test_LoopMove2()
{
char c[]="cplusplus_linux_socket_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove2(c,7);
cout<<"after LoopMove2(c,7) c[] is :"<<c<<endl;
}
int main()
{
cout<<"--------test_LoopMove1()-------\n";
test_LoopMove1();
cout<<"--------test_LoopMove2()-------\n";
test_LoopMove2();
}
/************************************
程序运行结果:
--------test_LoopMove1()-------
before: c[] is :future_money_wifi_parent_
after LoopMove1(c,7) c[] is :parent_future_money_wifi_
--------test_LoopMove2()-------
before: c[] is :cplusplus_linux_socket_
after LoopMove2(c,7) c[] is :socket_cplusplus_linux_
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
*************************************/
/****************************************
编程实现字符串的循环移位操作
函数头是这样的:
pStr是指向以'\0'结尾的字符串的指针
steps是要求移动的n
void LoopMove ( char * pStr, int steps )
{
// 请填充...
}
*****************************************/
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int MAX_LEN=255;
void LoopMove1( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen ( pStr ) ) = '\0';
strcpy( pStr, tmp );
}
void test_LoopMove1()
{
char c[]="future_money_wifi_parent_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove1(c,7);
cout<<"after LoopMove1(c,7) c[] is :"<<c<<endl;
}
void LoopMove2( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
memcpy( tmp, pStr + n, steps );
memcpy(pStr + steps, pStr, n );
memcpy(pStr, tmp, steps );
}
void test_LoopMove2()
{
char c[]="cplusplus_linux_socket_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove2(c,7);
cout<<"after LoopMove2(c,7) c[] is :"<<c<<endl;
}
int main()
{
cout<<"--------test_LoopMove1()-------\n";
test_LoopMove1();
cout<<"--------test_LoopMove2()-------\n";
test_LoopMove2();
}
/************************************
程序运行结果:
--------test_LoopMove1()-------
before: c[] is :future_money_wifi_parent_
after LoopMove1(c,7) c[] is :parent_future_money_wifi_
--------test_LoopMove2()-------
before: c[] is :cplusplus_linux_socket_
after LoopMove2(c,7) c[] is :socket_cplusplus_linux_
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
*************************************/
编程实现字符串的循环移位操作
函数头是这样的:
pStr是指向以'\0'结尾的字符串的指针
steps是要求移动的n
void LoopMove ( char * pStr, int steps )
{
// 请填充...
}
*****************************************/
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int MAX_LEN=255;
void LoopMove1( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen ( pStr ) ) = '\0';
strcpy( pStr, tmp );
}
void test_LoopMove1()
{
char c[]="future_money_wifi_parent_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove1(c,7);
cout<<"after LoopMove1(c,7) c[] is :"<<c<<endl;
}
void LoopMove2( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
memcpy( tmp, pStr + n, steps );
memcpy(pStr + steps, pStr, n );
memcpy(pStr, tmp, steps );
}
void test_LoopMove2()
{
char c[]="cplusplus_linux_socket_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove2(c,7);
cout<<"after LoopMove2(c,7) c[] is :"<<c<<endl;
}
int main()
{
cout<<"--------test_LoopMove1()-------\n";
test_LoopMove1();
cout<<"--------test_LoopMove2()-------\n";
test_LoopMove2();
}
/************************************
程序运行结果:
--------test_LoopMove1()-------
before: c[] is :future_money_wifi_parent_
after LoopMove1(c,7) c[] is :parent_future_money_wifi_
--------test_LoopMove2()-------
before: c[] is :cplusplus_linux_socket_
after LoopMove2(c,7) c[] is :socket_cplusplus_linux_
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
*************************************/
/****************************************
编程实现字符串的循环移位操作
函数头是这样的:
pStr是指向以'\0'结尾的字符串的指针
steps是要求移动的n
void LoopMove ( char * pStr, int steps )
{
// 请填充...
}
*****************************************/
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
const int MAX_LEN=255;
void LoopMove1( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen ( pStr ) ) = '\0';
strcpy( pStr, tmp );
}
void test_LoopMove1()
{
char c[]="future_money_wifi_parent_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove1(c,7);
cout<<"after LoopMove1(c,7) c[] is :"<<c<<endl;
}
void LoopMove2( char *pStr, int steps )
{
int n = strlen(pStr) - steps;
char tmp[MAX_LEN];
memcpy( tmp, pStr + n, steps );
memcpy(pStr + steps, pStr, n );
memcpy(pStr, tmp, steps );
}
void test_LoopMove2()
{
char c[]="cplusplus_linux_socket_";
cout<<"before: c[] is :"<<c<<endl;
LoopMove2(c,7);
cout<<"after LoopMove2(c,7) c[] is :"<<c<<endl;
}
int main()
{
cout<<"--------test_LoopMove1()-------\n";
test_LoopMove1();
cout<<"--------test_LoopMove2()-------\n";
test_LoopMove2();
}
/************************************
程序运行结果:
--------test_LoopMove1()-------
before: c[] is :future_money_wifi_parent_
after LoopMove1(c,7) c[] is :parent_future_money_wifi_
--------test_LoopMove2()-------
before: c[] is :cplusplus_linux_socket_
after LoopMove2(c,7) c[] is :socket_cplusplus_linux_
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
*************************************/
相关文章推荐
- 实现字符串右循环移位函数
- 编程之美的2.17,数组循环移位 & 字符串逆转(反转) Hello world Welcome => Welcome world Hello
- 一个小学奥数题的python实现(全排列,字符串循环移位,统计元素)
- 【程序员编程艺术】学习记录2:左旋转字符串之循环移位法
- 每天学习一点编程(14)(数组/字符串循环移位)
- 字符串操作之循环移位
- 实现字符串右循环移位函数。
- 字符串左旋--循环移位法(c++实现)
- 实现字符串右循环移位
- 用java实现数组的循环逐步移位操作
- 字符串循环移位 - 编程珠玑的一道题
- 编程之美3.1——字符串循环移位
- 编程:实现对字符串进行循环右移,例如:输入abcdefgh,循环右移34位,输出ghabcdef
- 编程实现字符串的循环右移
- 编程实现字符串的循环右移
- Java实现的文本字符串操作工具类实例【数据替换,加密解密操作】
- 字符串部分操作的函数实现
- 循环移位c语言实现
- 编程之美---->字符串移位包含问题
- Javascript实现能够双端操作的循环队列