Created
February 12, 2013 20:35
-
-
Save anonymous/4773146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define SIZE 254 | |
int main() | |
{ | |
char string_1[SIZE +1]; | |
printf( "Please enter a long string: " ); | |
fgets ( string_1, SIZE, stdin ); | |
size_t ln = strlen(string_1) - 1; | |
if (string_1[ln] == '\n') | |
string_1[ln] = '\0'; | |
if (string_1[ln] == '\r') | |
string_1[ln] = '\0'; | |
//printf( "You entered a very long string, %s", string_1 ); | |
char string_2[SIZE +1]; | |
printf( "Please enter a long string: " ); | |
fgets ( string_2, SIZE, stdin ); | |
size_t ln2 = strlen(string_2) - 1; | |
if (string_2[ln2] == '\n') | |
string_2[ln2] = '\0'; | |
if (string_2[ln] == '\r') | |
string_2[ln] = '\0'; | |
//printf( "You entered a very long string, %s", string_2 ); | |
printf("String 1 is %zu bytes long, and String 2 is %zu bytes long\n", ln, ln2); | |
char string_3[SIZE + 1]; | |
strncpy(string_3, string_1, strlen(string_1)/2); | |
string_3[strlen(string_1)/2] = '\0'; | |
strncpy((string_3 + strlen(string_3)), string_2, strlen(string_2)/2); | |
string_3[strlen(string_1)/2 + strlen(string_2)/2] = '\0'; | |
// strncpy(string_3, string_1, strlen(string_1)/2); | |
// string_3[strlen(string_1)/2] = '\0'; | |
// strncpy((string_3 + strlen(string_3)), string_2, strlen(string_2)/2); | |
// string_3[strlen(string_1)/2 - strlen(string_2)/2] = '\0'; | |
printf("%s", string_3); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment