高手帮忙来看看

无艳影 2011-08-17 10:33:14
我的程序用来实现字符串的循环右移位操作,内层while循环用来实现1次循环右移操作,如果循环右移K位,则将while循环重复执行K次。但第一遍的执行结果就不对,麻烦老鸟帮忙看看!

源代码:
#include <stdio.h>
#include <string.h>

#pragma warning(disable: 4786)

char *LoopRMove(char *str,int k);

void main(int argc,char *argv)
{
char name[] = "abcde";
char *p = name;
p = LoopRMove(p,29);
//printf("the current name is:%s\n",p);

return;
}

char *LoopRMove(char *str,int k)
{
char *p = str;
int len = 0;
while(*p != '\0')
{
len++;
p++;
}
p = p-len;

if(k%len == 0) return str;
k = k%len;

char tp1,tp2;
for(int i=0; i<k; i++)
{
tp1 = *(p+len-1);
while(*p++ != '\0')
{
tp2 = *p;
*p = tp1;
tp1 = tp2;
}
//让p重新指向字符串首字母
p = p-len;
printf("the current name is:%s\n",p);
}
return str;
}


执行结果:
the current name is:bcde
the current name is:
the current name is:蘹
the current name is:烫烫
Press any key to continue
...全文
130 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ulfsaar 2011-08-17
  • 打赏
  • 举报
回复
其实也可以这么写

for(int i=0; i<k; i++)
{
tp1 = *(p+len-1);
--p;
while(*++p != '\0')
{
tp2 = *p;
*p = tp1;
tp1 = tp2;
}
//让p重新指向字符串首字母
p = p-len;
printf("the current name is:%s\n",p);
}

无艳影 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ulfsaar 的回复:]

引用 10 楼 shi3590 的回复:
5L的while里没有++


++不一定要写在小括号内
[/Quote]
嗯,本来想直接写在小括号内会方便些,结果导致这样的错误!
不过也好,起码现在会对++的用法更熟悉些了!
Ulfsaar 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 shi3590 的回复:]
5L的while里没有++
[/Quote]

++不一定要写在小括号内
无艳影 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 shi3590 的回复:]

5L的while里没有++
[/Quote]

刚刚单步调试过了,确实如你所说,每一轮while循环的时候,都直接漏掉了第一个字母的处理!
非常感谢!
shi3590 2011-08-17
  • 打赏
  • 举报
回复
5L的while里没有++
zk___23456 2011-08-17
  • 打赏
  • 举报
回复
简单的说,你while循环错了,好好单步调试一下,就会发现的
无艳影 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shi3590 的回复:]

while条件判定之后你的第一个字母就丢掉了。
[/Quote]
丢掉了,你是说字符串的第一个字母丢掉了吗?
没有吧!我刚才试过5楼的方法,结果完全正确。

按5楼方法修改后,运行结果如下:
the current name is:eabcd
the current name is:deabc
the current name is:cdeab
the current name is:bcdea
Press any key to continue
shi3590 2011-08-17
  • 打赏
  • 举报
回复
while条件判定之后你的第一个字母就丢掉了。
无艳影 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ulfsaar 的回复:]
C/C++ code

while(*p != '\0')
{
tp2 = *p;
*p = tp1;
tp1 = tp2;
++p;
}


循环里面那段改成这样就行了
你要是用*p++的话,p在用完之后就变成下一个了,所以要分开写
[/Quote]

如果我改成这样呢:
for(int i=0; i<k; i++)
{
tp1 = *(p+len-1);
while(*p++ != '\0')
{
tp2 = *p;
*p = tp1;
tp1 = tp2;
}

//让p重新指向字符串首字母
p = p-len-1;
printf("the current name is:%s\n",p);
}
Ulfsaar 2011-08-17
  • 打赏
  • 举报
回复


while(*p != '\0')
{
tp2 = *p;
*p = tp1;
tp1 = tp2;
++p;
}

循环里面那段改成这样就行了
你要是用*p++的话,p在用完之后就变成下一个了,所以要分开写
无艳影 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 shi3590 的回复:]

C/C++ code

char *LoopRMove(char *str,int k);

void main(int argc,char *argv)
{
char name[] = "abcde";
char *p = name;
p = LoopRMove(p,29);
//printf("the current name is:%s\n",p);……
[/Quote]
我的while循环错在哪里呢?
无艳影 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 shi3590 的回复:]

你所谓的右移操作是什么?abcde右移4次变成什么?bcdea?
只要清楚你的右移目的,还是很好改的,而且计算字符串长度可以直接用strlen。
[/Quote]
对的,就是相当于a[i]的值变为a[i-k],abcde右移1位就变成eabcd
shi3590 2011-08-17
  • 打赏
  • 举报
回复

char *LoopRMove(char *str,int k);

void main(int argc,char *argv)
{
char name[] = "abcde";
char *p = name;
p = LoopRMove(p,29);
//printf("the current name is:%s\n",p);

return;
}

char *LoopRMove(char *str,int k)
{
char *p = str;
int len = 0;
while(*p != '\0')
{
len++;
p++;
}
p = p-len;

if(k%len == 0) return str;
k = k%len;

char tp1;
for(int i=0; i<k; i++)
{
tp1 = *(p+len-1);
for (int j=len-1;j>0;j--)
{
p[j]=p[j-1];
}
p[0]=tp1;
// while(*p++ != '\0')
// {
// tp2 = *p;
// *p = tp1;
// tp1 = tp2;
// }
// //让p重新指向字符串首字母
// p = p-len;
printf("the current name is:%s\n",p);
}
return str;
}
shi3590 2011-08-17
  • 打赏
  • 举报
回复
你所谓的右移操作是什么?abcde右移4次变成什么?bcdea?
只要清楚你的右移目的,还是很好改的,而且计算字符串长度可以直接用strlen。

70,022

社区成员

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

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