C语言字符串问题

题目要求: 输入3个字符串,按由大到小的顺序输出 要求用指针
我写的程序: #include<stdio.h>
#include<string.h>
void main()
{
char a[10],b[10],c[10],*p1,*p2,*p3;
char *temp;
int i;
printf("please input array a\n");
scanf("%s",a);
printf("please input array b\n");
scanf("%s",b);
printf("please input array c\n");
scanf("%s",c);
p1=a,p2=b,p3=c;
temp=a;
if(strcmp(p1,p2)>0)
{
for(i=0;*(p1+i)!='\0';i++)
*(temp+i)=*(p1+i);
*(temp+i)='\0';
for(i=0;*(p2+i)!='\0';i++)
*(p1+i)=*(p2+i);
*(p1+i)='\0';
for(i=0;*(temp+i)!='\0';i++)
*(p2+i)=*(temp+i);
*(p2+i)='\0';
}
if(strcmp(p1,p3)>0)
{
for(i=0;*(p1+i)!='\0';i++)
*(temp+i)=*(p1+i);
*(temp+i)='\0';
for(i=0;*(p3+i)!='\0';i++)
*(p1+i)=*(p3+i);
*(p1+i)='\0';
for(i=0;*(temp+i)!='\0';i++)
*(p3+i)=*(temp+i);
*(p3+i)='\0';
}
if(strcmp(p2,p3)>0)
{
for(i=0;*(p2+i)!='\0';i++)
*(temp+i)=*(p2+i);
*(temp+i)='\0';
for(i=0;*(p3+i)!='\0';i++)
*(p2+i)=*(p3+i);
*(p2+i)='\0';
for(i=0;*(temp+i)!='\0';i++)
*(p3+i)=*(temp+i);
*(p3+i)='\0';
}
printf("%s,%s,%s\n",a,b,c);
}
但是结果不正确 不知道什么原因 还希望各位能指教下..^^

另外 我觉得我写的复杂了. 有时间的朋友能不能给我写个简单的参考下? 谢谢了.

#include<stdio.h>

#include<string.h>

void strcpy1(char *a,char *b,char *c);

char d[50];

void sort();

int main()

{

 

char a[20],b[20],c[20];

printf("please input three  string1:");

gets(a);

printf("please input three  string2:");

gets(b);

printf("please input three  string3:");

gets(c);

strcpy1(a,b,c);

sort();

puts(d);

}

void strcpy1(char *a,char *b,char *c)

{

int i,j; 

for(i=0;i<strlen(a);i++)

d[i]=a[i];

for(j=0;j<strlen(b);j++,i++)

d[i]=b[j];

for(j=0;j<strlen(c);j++,i++)

d[i]=c[j];

d[i]='\0';

}

void sort()

{

int i,j;

char m;

for(i=0;i<strlen(d)-1;i++)

for(j=i+1;j<strlen(d);j++)

if(d[i]<d[j])

{

m=d[j];

d[j]=d[i];

d[i]=m;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-12-05
交换值的时候出错了··
其实没有必要做那么多工作的··只要交换指针指向的字符就行了··

#include<stdio.h>
#include<string.h>
void main()
{
char a[10],b[10],c[10],*p1,*p2,*p3;
char *temp;
printf("please input array a\n");
scanf("%s",a);
printf("please input array b\n");
scanf("%s",b);
printf("please input array c\n");
scanf("%s",c);
p1=a,p2=b,p3=c;
temp=a;
if(strcmp(p1,p2)>0)
{
temp = p1;
p1 = p2;
p2 = temp;
}
if(strcmp(p1,p3)>0)
{
temp = p1;
p1 = p3;
p3 = temp;
}
if(strcmp(p2,p3)>0)
{
temp = p2;
p2 = p3;
p3 = temp;
}
printf("%s,%s,%s\n",p1,p2,p3);
}本回答被提问者采纳
第2个回答  2009-12-05
指针....下个礼拜再来就能解答了

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网