024.自行製作字串函數 strcat()。

原發表者:Eternal
#include <stdio.h>

#define SIZE 80
char* strcat(char *, const char *);

void main()
{
    char string1[SIZE]={NULL}, string2[SIZE]={NULL};
    printf("Enter string1:\n");
    gets(string1);
    printf("Enter string2:\n");
    gets(string2);
    printf("%s\n", strcat(string1, string2));

}

char* strcat(char *s1, const char *s2)
{
    short i, j;
        for (i=0;i<SIZE;i++)
            if (s1[i] == NULL)
                for (j=0;j<SIZE;j++)
                {
                    s1[i+j] = s2[j];
                    if (s2[j] == NULL)
                        return s1;
                }
    return s1;
} 


作者:zha0
#include <stdio.h>
#define SIZE 80
char* strcat(char *, const char *);
void main()
{

    char string1[SIZE]={NULL}, string2[SIZE]={NULL};
    printf("Enter string1:\n");
    gets(string1);
    printf("Enter string2:\n");
    gets(string2);
    printf("%s\n", strcat(string1, string2));
}

char* strcat(char *s1, const char *s2)
{
    char *pt;
    pt=s1;
    while (*s1++);
    s1--;
    while (*s1++=*s2++);
    return pt;
}

zha0提供另外一種寫法
void strCpy(char *s, char *t)
{
if (*t = *s) strCpy(s + 1, t + 1);
}

作者:zoe
char * cat(char *s1, char *s2)
{
   int i, j; 
   for ( i = 0;; i++ )
   {
       if ( s1[i] == '' )
       {
           for ( j = 0;; j++ )
           {
               s1[i] = s2[j];
               i++;
               
               if ( s2[j] == '' )
                   break;
           }
           break;
       }
   }
   
   return s1;
}

在此感謝當年在數位高手(NBP)分享的朋友,本人將資料彙整於此並盡量將當初發表者名稱註記。
文章標籤
全站熱搜
創作者介紹
創作者 NBPBlog 的頭像
NBPBlog

NBP部落格-分享是為了成長

NBPBlog 發表在 痞客邦 留言(0) 人氣(953)