/************************************************************************* > File Name: chuan.c > Author: heathclifff > Mail: --------------------- > Created Time: 2016年04月16日 星期六 21時00分33秒 ************************************************************************/ #include<stdio.h> #define MAXSTRLEN 255 typedef unsigned int SString[MAXSTRLEN]; char Contant(SString T, SString s1, int length_1, SString s2, int length_2, int length) { int i,flag; if(length <= MAXSTRLEN){ for(i = 0;i < length_1;i++) T[i] = s1[i]; //將s1中的值複製到T的前半部分 for(i = i;i <= length;i++) T[i] = s2[i-length_1]; //將s2中的值複製到T的後半部分 printf("串鏈接成功\n"); } else if(length > MAXSTRLEN && length_1 <= MAXSTRLEN){ for(i = 0; i< length_1;i++) T[i] = s1[i]; for(i; i <= MAXSTRLEN;i++) T[i] = s2[i-length_1]; if(i-length_1-1 != 0) printf("串s1與串s2的前%d部分已經複製到T中\n",i-length_1-1); else printf("串s1已經複製到T中,串s2殯天了->_->\n"); } else if(length_1 > MAXSTRLEN){ for(i = 0;i < MAXSTRLEN;i++) T[i] = s1[i]; printf("S1的前%d部分已經複製到T中\n",i); } } int main(void) { int length_1 = 0, length_2 = 0; int length = 0;//計算串的長度 int i; SString s1, s2, T; printf("\n請輸入串s1的值,以65535爲結束標誌\n"); for(i = 0;i < MAXSTRLEN;i++){ scanf("%d",&s1[i]); if(s1[i] == 65535){ length_1 ++; break; } length_1 ++; } printf("\n請輸入串s2的值,以65535爲結束標誌"); for(i = 0;i < MAXSTRLEN;i++){ scanf("%d",&s2[i]); if(s2[i] == 65535){ length_2 ++; break; } length_2 ++; } length_1 = length_1 - 1; length_2 = length_2 - 1; printf("length_1 = %d\n",length_1); printf("length_2 = %d\n",length_2); length = length_1 + length_2; Contant(T, s1, length_1, s2, length_2, length); printf("合併後的串爲:\n"); for(i = 0;i < length;i++) printf("[%d]",T[i]); printf("\n"); printf("串的長度爲%d\n",length); return 0; }