連線兩個字串,不用strcat
阿新 • • 發佈:2019-02-05
#include <stdio.h>
void main()
{
void str_link(char str1[], char str2[]); //宣告str_link函式
char s1[20], s2[20];
printf("任意輸入兩個字串,編寫函式,實現串的連線。函式原型如下:\n");
printf("void str_link(char str1[ ],char str2[ ]);將串2連線在串1的末尾,形成一個新串\n");
printf("please input s string:\n");
scanf("%s", s1); //輸入第一個字串
printf("please input a string:\n");
scanf("%s", s2); //輸入第二個字串
str_link(s1, s2); //呼叫str_link函式
printf("%s\n\n", s1);
}
void str_link(char str1[], char str2[]) //定義str_link函式
{
int i = 0, j = 0, m = 0;
while (str1[i] != '\0') //計算str1的個數
{
m++;
i++;
}
do
{
str1[m] = str2[j]; //將str2複製到str1
m++;
j++;
} while (str2[j] != '\0');
str1[m] = '\0';
}
void main()
{
void str_link(char str1[], char str2[]); //宣告str_link函式
char s1[20], s2[20];
printf("任意輸入兩個字串,編寫函式,實現串的連線。函式原型如下:\n");
printf("void str_link(char str1[ ],char str2[ ]);將串2連線在串1的末尾,形成一個新串\n");
printf("please input s string:\n");
scanf("%s", s1); //輸入第一個字串
printf("please input a string:\n");
scanf("%s", s2); //輸入第二個字串
str_link(s1, s2); //呼叫str_link函式
printf("%s\n\n", s1);
}
void str_link(char str1[], char str2[]) //定義str_link函式
{
int i = 0, j = 0, m = 0;
while (str1[i] != '\0') //計算str1的個數
{
m++;
i++;
}
do
{
str1[m] = str2[j]; //將str2複製到str1
m++;
j++;
} while (str2[j] != '\0');
str1[m] = '\0';
}