该代码是使用库函数复制和连接字符串。我使用的代码:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100
char* newStrCpy(char string1[],char string2[])
{
int i=0;
while(string1[i]!='\0')
{
string2[i]=string1[i];
i++;
}
return (string2);
}
char* newStrCat(char destination[],char arr[])
{
int a=0;
int destlen=0;
while(destination[destlen]!='\0')
{
destlen ++;
}
while(a<(MAX-destlen))
{
destination[(destlen+a)]=arr[a];
a++;
}
return destination;
}
void main()
{
char string1[MAX],string2[MAX];
int i=0,j=0;
char bak[5]="\n is";
char som[50]=" the concatenated array.";
fflush(stdin);
printf("Enter a string:\n");
scanf("%[^\n]s",&string1);
newStrCpy(string1,string2);
printf("The copied string is:\n");
while(string2[i]!='\0')
{
printf("%c",string2[i]);
i++;
}
newStrCat(string2,bak);
newStrCat(string2,som);
printf("\nThe conctenated string is:\n");
while(string2[j]!='\0')
{
printf("%c",string2[j]);
j++;
}
fflush(stdout);
getch();
}
我得到的输出:
Enter a string:
Welcome!!
复制的字符串是:
Welcome!!
he concatenated string is:
Welcome!!
is the concatenated string