程序代码:
#include <stdio.h>
#include <stdlib.h>
const int MAX_LENGTH = 99;
int my_gets(char str[], int maxLength);
int my_puts(const char str[], int maxLength);
char* my_strcat(char s1[], char s2[], int maxLength);
int main(void)
{
char str1[MAX_LENGTH + 1], str2[MAX_LENGTH + 1];
my_gets(str1, MAX_LENGTH);
my_gets(str2, MAX_LENGTH);
putchar('\n');
my_puts(my_strcat(str1, str2, MAX_LENGTH), MAX_LENGTH);
putchar('\n');
my_puts("\nPress ENTER to continue...", MAX_LENGTH);
fflush(stdin);
getchar();
return EXIT_SUCCESS;
}
int my_gets(char str[], int maxLength)
{
int count;
int ch = '\0';
for (count = 0; (count < maxLength) && (ch != EOF) && (ch != '\n'); ++count)
{
if ((ch = getchar()) != EOF)
{
str[count] = ch;
}
}
if (count < maxLength)
{
--count;
}
str[count] = '\0';
return count;
}
int my_puts(const char str[], int maxLength)
{
int count;
for (count = 0; (str[count] != '\0') && (count < maxLength); ++count)
{
putchar(str[count]);
}
return count;
}
char* my_strcat(char s1[], char s2[], int maxLength)
{
int count;
int index;
for (count = 0; (count < maxLength) && (s1[count] != '\0'); ++count)
{
;
}
if (count < maxLength)
{
index = 0;
while ((count < maxLength) && (s2[index] != '\0'))
{
s1[count++] = s2[index++];
}
}
s1[count] = '\0';
return s1;
}