Skip to content

Instantly share code, notes, and snippets.

@sirovenmitts
Forked from anonymous/gist:4773146
Last active December 12, 2015 12:49
Show Gist options
  • Save sirovenmitts/4774855 to your computer and use it in GitHub Desktop.
Save sirovenmitts/4774855 to your computer and use it in GitHub Desktop.
Brian, here is my quick take on your problem. The code works as expected, but makes some assumptions, namely: Input will be less than 255 bytes long; and, newlines (\n) and carriage returns (\r) will only be at the end of input. God help you if they are somewhere in the middle.
// ask for two strings and join the first half of the first string
// with the second half of the second string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 255
bool getInput( char *aString ) {
printf( "Please enter a long string: " );
if( fgets( aString, SIZE, stdin ) == aString ) {
int lastElement = strlen( aString ) - 1;
if( aString[ lastElement ] == '\n' || aString[ lastElement ] == '\r' ) {
aString[ lastElement ] = '\0';
}
return true;
}
return false;
}
void printStringSize( char *aString ) {
printf( "The string \"%s\" is %zu bytes long\n", aString, strlen( aString ) );
}
int main( void ) {
char
firstString[ SIZE ],
secondString[ SIZE ],
thirdString[ SIZE ];
if( getInput( firstString ) ) {
if( getInput( secondString ) ) {
printStringSize( firstString );
printStringSize( secondString );
int firstStringHalfwidth = strlen( firstString ) / 2;
int secondStringHalfwidth = strlen( secondString ) / 2;
int lastHalfOfSecondString = strlen( secondString ) - secondStringHalfwidth;
strncpy( thirdString, firstString, firstStringHalfwidth );
strncpy(
&thirdString[ firstStringHalfwidth ],
&secondString[ secondStringHalfwidth ],
lastHalfOfSecondString );
printf( "%s\n", thirdString );
return 0;
}
}
err( 1, "For some reason I was unable to get input from you. Oh well.\n" );
return 1;
}
@sirovenmitts
Copy link
Author

Background

In case it's not noted and someone else comes across this: the fellow who uploaded this Gist would like to accept two strings from the user, and then wants to join the first half of the first string and the second half of the second string and print them.

TL;DR

There are many things I want to comment on after our last chat, but the only real issue your code has is its math. strncpy needs 3 things; An address where it should start copying elements; An address it should start copying elements from; And a number of bytes to copy.

You specify (not very clearly) the first thing when you pass (string_3 + strlen(string_3)), but then you pass in the address of the first element of string_2. This means strncpy will copy the first strlen(string_2) / 2 bytes from string_2 which is not what you want. You'll need to adjust the second argument so that strncpy knows to start at the element strlen(string_2) / 2 of the array string_2.

Additionally, you need to adjust the 3rd parameter you pass in (strlen(string_2) / 2); your program will only technically "work" with strings that have an even length. You'll need to adjust the 3rd argument to be the entire length of string_2, less the index you wished to start at (strlen(string_2) / 2). If you don't, then you'll be missing the last character from any odd length string.

Disclaimer

I am not a C wizard. If I got anything wrong, please let me know at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment