C语言 用指针方法 输入3个字符串 按由小到大顺序输出

我的程序编译连接都没问题,运行的时候 ,输入一个字符串,然后回车就报错,是哪里错了呢。。。。各位大神给看看。。。

#include "stdafx.h"
#include "stdio.h"
#include "string.h"

void order(char *p1,char *p2,char *p3)
{
char *t;
if(strcmp(p1,p2)>0)
{t=p1;p1=p2;p2=t;}

if(strcmp(p3,p1)>0)
{t=p1;p1=p3;p3=t;}

if(strcmp(p3,p2)>0)
{t=p2;p2=p3;p3=t;}
}

int main(int argc, char* argv[])
{

char *p1=NULL,*p2=NULL,*p3=NULL;
scanf("%s\n",p1);
fflush(stdin);

scanf("%s\n",p2);
fflush(stdin);

scanf("%s\n",p3);
fflush(stdin);

order(p1,p2,p3);

printf("%s\n%s\n%s\n",p1,p2,p3);

return 0;
}

可以使用三个数组,或者是一个二维数组来存储字符串,同时定义一个指针数组,指向三个字符串的首地址。然后对指针数组进行排序。

代码如下:

#include <stdio.h>
#include <string.h>
int main()
{
    char buf[3][100];
    char *p[3] = {buf[0],buf[1], buf[2]};
    int i,j;
    
    for(i = 0; i < 3; i ++)
        scanf("%s", p[i]);//输入三个字符串。
    for(i = 0; i < 2; i ++)//排序。
        for(j = i+1; j < 3; j ++)
        {
            if(strcmp(p[i], p[j]) > 0)
            {
                char * t = p[i];
                p[i] = p[j];
                p[j] = t;
            }
        }
     for(i = 0; i < 3; i ++)
        printf("%s\n", p[i]);//输出排序后的三个字符串。
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-05-27

#include&lt;stdio.h&gt;

#include&lt;string.h&gt;

int main()

{

char str1[10],str2[20],str0[10];

printf("please input 3 strings");

gets(str1);

gets(str2);

gets(str0);

if(strcmp(str1,str2)&gt;0)swap(str1,str2);/*字符串比较函数*/

if(strcmp(str2,str0)&gt;0)swap(str2,str0);

if(strcmp(str1,str0)&gt;0)swap(str1,str0);

printf("Now the otrder is:")

printf("%s\n%s\n%s"\nstr1,str2,str0);

return 0;

}

void swap(char*p1,*p2)

{

char str[10];

strcpy(str,p1);

strcpy(p1,p2);

strcpy(p2,str);

}

扩展资料:

strcpy用法:

1、strcpy(a+1,b+2)相当于将a[1]及它后面的内容复制为b[2]及它后面的内容。b[2]及后面为“2”,因此复制后a为“a2”;

2、strcat(a,c+1)相当于在a的末尾加上c[1]及其后面的部分,也就是“yz”。故运行后a为“a2yz”

strcpy把从src地址开始且含有'\0'结束符的字符串复制到以dest开始的地址空间,返回值的类型为char*。

strcat把src所指向的字符串(包括“\0”)复制到dest所指向的字符串后面(删除*dest原来末尾的“\0”)。

第2个回答  推荐于2017-09-01
指针没有分配空间可以使用吗?
定义指针是不分配空间的,在使用前你得初始化,让它指向确定的地址才可以后续使用。
函数中是没法更改传入变量指针地址的!但可以更改其中的内容.
你的比较好像有问题,得不到所需要的:“从小到大”
#include "stdio.h"
#include "string.h"
int main(int argc, char* argv[])
{
char *t;
char *p1=NULL,*p2=NULL,*p3=NULL;
char ch1[20]={0},ch2[20]={0},ch3[20]={0};

p1=ch1;
p2=ch2;
p3=ch3;

printf("No1:");
scanf("%s",p1);
fflush(stdin);
printf("No2:");
scanf("%s",p2);
fflush(stdin);
printf("No3:");
scanf("%s",p3);
fflush(stdin);

if(strcmp(p1,p2)>0)
{t=p1;p1=p2;p2=t;}

if(strcmp(p1,p3)>0)
{t=p1;p1=p3;p3=t;}

if(strcmp(p2,p3)>0)
{t=p2;p2=p3;p3=t;}

printf("%s\n%s\n%s\n",p1,p2,p3);

return 0;
}本回答被提问者采纳
第3个回答  2019-10-12

#include<stdio.h>

#include<string.h>

int main()

{ char s[3][101],*p1,*p2;

  int i;

  p1=p2=s[0];

  for(i=0;i<3;i++)

    gets(s[i]);

  if(strcmp(s[1],p1)>0)p1=s[1];

    else if(strcmp(s[1],p2)<0)p2=s[1];

  if(strcmp(s[2],p1)>0)p1=s[2];

    else if(strcmp(s[2],p2)<0)p2=s[2]; 

  printf("Max=%s\nMin=%s\n",p1,p2);

  return 0;

}

第4个回答  2012-03-15
char *p1=NULL,*p2=NULL,*p3=NULL;
p1,p2,p3都没有分配空间,所以不能存储数据。
scanf("%s\n",p1); //\n别放在这,这是输入,不是输出
if(strcmp(p1,p2)>0)
{t=p1;p1=p2;p2=t;} //字符串不能这样简单的交换,用strcpy,当然t也要分配空间
相似回答