Easy Tutorial
❮ C Examples Pyramid Pattern C Exercise Example24 ❯

C Exercise Example 86

C Language Classic 100 Examples

Title: String Concatenation Program.

Program Analysis: None.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* strconnect(char *str1, char *str2);

int main()
{
    char str1[20], str2[20];
    char *str;
    puts("Please enter two strings, separated by a newline:");
    scanf("%s%s", str1, str2);
    str = strconnect(str1, str2);
    puts("The concatenated string is:");
    puts(str);
    return 0;
}

char* strconnect(char *str1, char *str2)
{
    char *str;
    str = (char*)malloc(strlen(str1) + strlen(str2) + 1);
    str[0] = '\0';
    strcat(str, str1);
    strcat(str, str2);
    return str;
}

C Language Classic 100 Examples

❮ C Examples Pyramid Pattern C Exercise Example24 ❯