C Exercise Example 86

100 Classic C Examples

Title:A program that concatenates two strings.

Program analysis:None.

Program source code:

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. 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 Enter:"); 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; }

100 Classic C Examples

other extensions