请教编程题:编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh”

greatriver007 2013-03-15 05:44:41
请教编程题:编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh”
//pStr是指向以'\0'结尾的字符串的指针
//steps是要求移动的n

void LoopMove ( char * pStr, int steps )
{
 //请填充...
}

答案1:
void LoopMove ( 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 );
}
答案2:
void LoopMove ( 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 );
}

运行到红色字符的时候就程序崩溃了
求大侠解答。
...全文
2045 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
greatriver007 2013-03-16
  • 打赏
  • 举报
回复
留上的 显示错误 我没有权限修改 应该是这样的

void LoopMove (char *pStr, int steps )
{
	int n = strlen( pStr ) - steps;
	char tmp[100];
	strcpy ( tmp, pStr + n );
	strcpy ( tmp + steps, pStr);
	*( tmp + strlen (pStr)) ='\0';
	strcpy( pStr, tmp );
}


int _tmain(int argc, _TCHAR* argv[])
{

	int i=2;
    //char aa[10] = "abcdefgh"; 
	char *aa = "abcdefgh"; 
    LoopMove(aa,i);

	cout<<"aa[]="<<aa<<endl;
	system("pause");
	return 0;
}
现在认真考虑才知道 其中的区别 1、char aa[10] = "abcdefgh"; 2、char *aa = "abcdefgh"; 1 数组变量是在 数据存储区 2 指针aa 指向的地址数据是常量 数据不可以修改 不知道我这样理解正确不的
greatriver007 2013-03-16
  • 打赏
  • 举报
回复
哦 谢谢你 确实是定义的数组小了 问题解决了。 其实我昨天测试的时候是这样的

void LoopMove (char *pStr, int steps )
{
	int n = strlen( pStr ) - steps;
	char tmp[100];
	strcpy ( tmp, pStr + n );
	strcpy ( tmp + steps, pStr);
	*( tmp + strlen (pStr)) ='\0';
	strcpy( pStr, tmp );
}


int _tmain(int argc, _TCHAR* argv[])
{

	int i=2;
    //char aa[10] = "abcdefgh"; 
	char *aa = "abcdefgh"; 
    LoopMove(aa,i);

	cout<<"aa[]="<<aa<<endl;
	system("pause");
	return 0;
}
现在认真考虑才知道 其中的区别 1、char aa[10] = "abcdefgh"; 2、char *aa = "abcdefgh"; 1 数组变量是在 数据存储区 2 指针aa 指向的地址数据是常量 数据不可以修改 不知道我这样理解正确不的
liao05050075 2013-03-16
  • 打赏
  • 举报
回复

void LoopMove (char *pStr, int steps )
{
    int n = strlen( pStr ) - steps;
    char tmp[10];
    strcpy ( tmp, pStr + n );
    strcpy ( tmp + steps, pStr); --》越界了。 steps==2时,这里执行完后tmp为ghabcdefgh 共10个字符,最后还加上一个'\0',11个,超过了tmp[10]。把tmp数组改大点再试试
    *( tmp + strlen (pStr)) ='\0';
    strcpy( pStr, tmp );
}
 
greatriver007 2013-03-16
  • 打赏
  • 举报
回复
代码如下:

void LoopMove (char *pStr, int steps )
{
int n = strlen( pStr ) - steps;
char tmp[10];
strcpy ( tmp, pStr + n );
strcpy ( tmp + steps, pStr);
*( tmp + strlen (pStr)) ='\0';
strcpy( pStr, tmp );
}


int _tmain(int argc, _TCHAR* argv[])
{

int i=2;
char aa[10] = "abcdefgh";
LoopMove(aa,i);

cout<<"aa[]"<<aa<<endl;
system("pause");
return 0;
}


nice_cxf 2013-03-15
  • 打赏
  • 举报
回复
贴全代码,目测是修改了常量导致的
starytx 2013-03-15
  • 打赏
  • 举报
回复
很有可能pStr访问越界了,确保tmp的长度不超过pStr指向的数组长度
小黑小宝 2013-03-15
  • 打赏
  • 举报
回复
跟踪看看数组越界没

70,018

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧