C언어 concat()

1 개요[ | ]

C언어 concat()
#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // strlen
char* concat(const char *s1, const char *s2) {
    char *res = malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(res, s1);
    strcat(res, s2);
    return res;
}
int main() {
    char* str1 = "Hello,";
    char* str2 = " World";
    char* output = concat(str1, str2);
    printf(output); // Hello, World
}
#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // strlen
char* concat(const char *s1, const char *s2) {
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);
    char *res = malloc(len1 + len2 + 1);
    memcpy(res, s1, len1);
    memcpy(res + len1, s2, len2 + 1);
    return res;
}
int main() {
    char* str1 = "Hello,";
    char* str2 = " World";
    char* output = concat(str1, str2);
    printf(output); // Hello, World
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}