Skip to content

Instantly share code, notes, and snippets.

@ktnyt
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktnyt/0bcc5338fd3a8b7d57c1 to your computer and use it in GitHub Desktop.
Save ktnyt/0bcc5338fd3a8b7d57c1 to your computer and use it in GitHub Desktop.
Let's nyan
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 256
int main() {
char str1[MAXLINE];
char str2[MAXLINE];
char str3[MAXLINE];
char *s;
char tmp[MAXLINE*4]; // 4回結合用
printf("String 1: ");
fgets(str1, MAXLINE, stdin);
printf("String 2: ");
fgets(str2, MAXLINE, stdin);
printf("String 3: ");
fgets(str3, MAXLINE, stdin);
s = strchr(str1, '\n');
if(s != NULL) {
*s = '\0';
}
s = strchr(str2, '\n');
if(s != NULL) {
*s = '\0';
}
s = strchr(str3, '\n');
if(s != NULL) {
*s = '\0';
}
// Method 1
strcpy(tmp, ""); // tmpを初期化
strcpy(tmp, str1);
strcat(tmp, str2);
strcat(tmp, str3);
printf("%s\n", tmp);
// Method 2
strcpy(tmp, ""); // tmpを初期化
strcat(strcat(strcat(tmp, str1), str2), str3);
printf("%s\n", tmp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment