题目是:从键盘上输入两个字符串,对两个字符串分别排序;然后将它们合并,合并后的字符串按ASCII码值从小到大排序,并删去相同字符。
编的程序:
#include <stdio.h>
#include <stdlib.h>
strmerge(char *a,char *b,char *c)
{char t,*w;
while(*a!='\0'&&*b!='\0')
{
if(*a<*b)
{
t=*a;
a++;}
else if(*a>*b)
{
t=*b;
b++;}
else
{
t=*a;
a++;
b++;}
}
if(*w=='\0')
{*w=t;}
else if(t!=*w)
{*(++w)=t;}
if(*a!='\0')
{
while(*a!='\0')
{if(*a!=*w)
{*(++w)=*a;
a++;}
else
{a++;}
}
}
}
strsort(char *s)
{ int i,j,n;
char t,*w;
w=s;
for(n=0;*w!=0;n++)
{w++;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(s[i]>s[j])
{ t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
}
}
int main(int argc, char *argv[])
{
char s1[100],s2[100],s3[100];
printf("Please input the first string:");
scanf("%s",s1);
printf("Please input the second string:");
scanf("%s",s2);
strsort(s1);
strsort(s2);
printf("The sorted string1:%s\n",s1);
printf("The sorted string2:%s\n",s2);
s3[0]='\0';
strmerge(s1,s2,s3);
printf("The new string:%s\n",s3);
system("PAUSE");
return 0;
}
哪里出错了?为什么程序会崩溃?
对百度的格式无语了。直接下载我的附件吧。
同学,做人要厚道。提问请采纳好吗
#include <stdio.h>
#include <stdlib.h>
void strmerge(char *a,char *b,char *c)
{char t,*w;
while(*a!='\0'&&*b!='\0')
{
while(*a==*(a+1))a++;
while(*b==*(b+1))b++;
if(*a<*b)
{ *(c++)=*(a++);}
else if(*a>*b)
{*(c++)=*(b++);}
else
{*(c++)=*(a++);
b++;}
}
while(*a!='\0')
{*(c++)=*(a++);};
while(*b!='\0')
{*(c++)=*(b++);}
*c='\0';
}
void strsort(char *s)
{ int i,j,n;
char t,*w;
w=s;
for(n=0;*w++;n++);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(s[j]>s[j+1])
{ t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
}
int main(int argc, char *argv[])
{
char s1[100],s2[100],s3[100];
printf("Please input the first string:");
scanf("%s",s1);
printf("Please input the second string:");
scanf("%s",s2);
strsort(s1);
strsort(s2);
printf("The sorted string1:%s\n",s1);
printf("The sorted string2:%s\n",s2);
s3[0]='\0';
strmerge(s1,s2,s3);
printf("The new string:%s\n",s3);
system("PAUSE");
return 0;
}
一时手痒,自己写了个。
输入是用的命令行参数 app_name str1 str2;然后对str1,str2排序,然后合并
可是你的这个程序也出错了呀?编译说44、45行的for循环有问题。能不能还请帮我看一下我原来的程序呀?
追答这个for循环是要用C99标准支持的。估计你用的C89.
这里修改可以把变量提到括号外定义。
int i;
int j;
for(i = 0; i < xx; ++i) {
for(j = 0; j < xxx; ++j) {
//hahah
}
}