习题 7.6 写一个函数,将两个字符串连接。

C程序设计(第四版) 谭浩强 习题7.6 个人设计

习题 7.6 写一个函数,将两个字符串连接。

代码块:

方法1:

#include 
#include 
char link(char x[], char y[]);                //定义连接函数
main()
{
    char a[30]={"How do "};
    char b[]={"you do?"};
    a[30]=link(a, b);                         //调动连接函数
    puts(a);                                  //输出连接后的字符串
    return 0;
}
//连接函数
char link(char x[], char y[])
{
    int m, n, i, j;
    m=strlen(x);
    n=strlen(y);
    for (i=m, j=0; ireturn x[i];
}

方法2:

#include 
#include 
void input(char st[], int n);                   //定义输入函数
void output(char st[]);                         //定义输出函数
void connect(char x[], char y[]);               //定义连接函数
int main()
{
    char s1[20], s2[10];
    input(s1, 1);                               //调用输入函数,输入字符串1
    input(s2, 2);                               //调用输入函数,输入字符串2
    connect(s1, s2);                            //调用连接函数
    output(s1);                                 //调用输出函数
    return 0;
}
//输入函数
void input(char st[], int n)
{
    printf("Please enter string%d: ", n);
    gets(st);
}
//输出函数
void output(char st[])
{
    printf("The final string: %s\n", st);
}
//连接函数
void connect(char x[], char y[])
{
    int m=strlen(x);
    int n=strlen(y);
    for (int i=m, j=0; i'\0';
}

你可能感兴趣的:(C程序设计,(第四版),谭浩强,课后答案)