鱼C论坛

 找回密码
 立即注册
查看: 3044|回复: 3

字符逆序排放

[复制链接]
发表于 2020-2-23 02:12:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
printf("qingshuru:\n");
gets(str);
sub(str,strlen(str));
puts(str);
return 0;

}

void sub(char*p1;int n)
{

char*pa=p1+n-1,temp;
while(p1<p2);
{
   temp=*p1++;
   *p1++=*p2;
   *p2--=temp;
   

}


}


这些代码哪错了?为什么运行不了?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-23 08:11:39 | 显示全部楼层
1. sub函数要先申明才能调用
2. while()语句后面的;号要产出,否则是while空语句
3. 变量名pa应该是p2

调试运行正常的代码如下:

#include<stdio.h>
#include<string.h>

void sub(char*p1, int n);

int main()
{
char str[20];
printf("qingshuru:\n");
gets(str);
sub(str,strlen(str));
puts(str);
return 0;

}

void sub(char*p1, int n)
{

char*p2=p1+n-1,temp;
while(p1<p2)
{
   temp=*p1;
   *p1++=*p2;
   *p2--=temp;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-23 10:52:02 | 显示全部楼层
还是不细心
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-23 11:00:56 | 显示全部楼层
本帖最后由 jackz007 于 2020-2-23 11:10 编辑
  1. #include<stdio.h>
  2. #include<string.h>

  3. void sub(char * p1)
  4. {
  5.         char  * p2 = p1 + strlen(p1) - 1 , c                               ;
  6.         for(; p2 > p1 ; c = * p1 , * p1 = * p2 , * p2 = c , p1 ++ , p2 --) ;
  7. }        

  8. int main()
  9. {
  10.         char str[20]           ;
  11.         printf("qingshuru:\n") ;
  12.         gets(str)              ;
  13.         sub(str)               ;
  14.         puts(str)              ;
  15. }
复制代码

        编译、运行实况
  1. C:\Bin>g++ -o x x.c

  2. C:\Bin>x
  3. qingshuru:
  4. Hello , World !
  5. ! dlroW , olleH

  6. C:\Bin>
复制代码


        这个代码看上去是不是更加清爽呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 08:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表