/* 2.cpp .2. 编写一个函数,
把字符串中所有的字符都前移一个位置,
而串中的第一个字符移到最后。并编写主程序进行测试。*/
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
//#1.返回前移后的字符串
/*
string move(string s)
{
}
*/
//#2.字符串数组作为函数参数
void move(char* s)
{
int n = strlen(s);
char* str = new char[n];
strncpy_s(str,n, s, n - 1);
strncpy_s(str,n, s, 1);
strncpy_s(s,n, str, n);
//cout << str << endl;z
delete[]str;
}
/*string转换成char的方法
const char* pstr=str.c_str();
*/
int main()
{
string s;
cin >> s;
const char *str = s.c_str();
move(str);
cout << str << endl;
return 0;
}
1.你的move 刚好跟系统的函数重名,调用了是系统的而不是你的move(你可以在函数添加打印验证);
2.你可以改成mymove,然后你可能会发现你的程序报错,而且运行结果也是不对的
更新:
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
//#1.返回前移后的字符串
/*
string move(string s)
{
}
*/
//#2.字符串数组作为函数参数
void mymove(char *dest,char* s)
{
int i,n = strlen(s);
for(i=0;i<(n-1);i++)
dest[i] = s[i+1];
dest[i++] = s[0];
dest[i] = '\0';
}
/*string转换成char的方法
const char* pstr=str.c_str();
*/
int main()
{
string s;
cin >> s;
const char *str = s.c_str();
char *dest = new char[s.size()+1];
memset(dest,0x0,s.size()+1);
mymove(dest,(char *)str);
cout << dest << endl;
return 0;
}