自定义字符串程序(老是出错,就是不懂哪里有错,求大神们指点一下)
#include<stdio.h>void strcat(char s1[],char s2[]);
void strcmp(char s1[],char s2[]);
void strcpy(char s1[],char s2[]);
void strlen(char s1[],char s2[]);
void main()
{
char s1[20],s2[20];
printf("please 1th string:");
scanf("%s",&s1);
printf("please 2th string:");
scanf("%s",&s2);
strcat(s1,s2);
strcmp(s1,s2);
strcpy(s1,s2);
strlen(s1,s2);
}
void strcat(char s1[],char s2[])
{
int i,j;
char s[40];
for(i=0;s1[i]!='\0';i++)
{
s[i]=s1[i];
}
for(j=i+1;s2[j]='\0';j++)
{
s[i+j]=s2[j];
}
printf("\n");
printf("连接后的两串字符为:%s",s);
}
void strcmp(char s1[],char s2[])
{
int i;
for(i=0;s1[i]!='\0',s2[i]!='\0';i++)
{
if(s1[i]>s2[i])
printf("strcmp<s1,s2>=1\n");
if(s1[i]<s2[i])
printf("strcmp<s1,s2>=-1\n");
if(s1[i]=='\0'&&s1[i]==s2[i])
printf("strcmp<s1,s2>=0\n");
}
}
void strcpy(char s1[],char s2[])
{
int i;
s2[i]=s1[i];
printf("s1复制到s2后的字符串:");
printf("%s\n",s2);
}
void strlen(char s1[],char s2[])
{
int i;
int a=0,b=0;
for(i=0;*s1!='\0';s1++)
{
a++;
}
for(i=0;*s2!='\0';s2++)
{
b++;
}
printf("a=%d,b=%d\n",a,b);
}