您的位置:首页 > 编程语言

编程实现字符串的循环移位操作

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.

*************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  笔试面试 编程 C++ CC++ C