Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created December 4, 2010 11:56
Show Gist options
  • Save johnhmj/728128 to your computer and use it in GitHub Desktop.
Save johnhmj/728128 to your computer and use it in GitHub Desktop.
2. 設計一主程式,並呼叫一個副程式StrCat()可以將字串a[]與b[]連接成c[]。
主程式:(1) 輸入兩個字串資料a[]與b[]。(2) 呼叫StrCat (),印出a[]、b[]、c[]之內容。
副程式:StrCat (),將字串a[]與b[]連接成c[],並將結果傳回主程式。
#include <stdio.h>
#include <stdlib.h>
#define BUFFERSIZE 512
void StrCat(char* dest, const char* str1, const char* str2);
//
int main(int argc, char* argv[])
{
char a[BUFFERSIZE];
char b[BUFFERSIZE];
char c[BUFFERSIZE];
printf("Input a string: "), gets(a);
printf("Input b string: "), gets(b);
StrCat(c, a, b);
printf("C = %s\n", c);
system("PAUSE");
return (0);
}
void StrCat(char* dest, const char* str1, const char* str2)
{
unsigned int i = 0, j = 0;
for (; str1[i] != '\0'; i ++, j ++)
{
dest[j] = str1[i];
}
for (i = 0; str2[i] != '\0'; i ++, j ++)
{
dest[j] = str2[i];
}
dest[j] = '\0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment