Skip to content

Instantly share code, notes, and snippets.

@spedru
Created November 24, 2014 21:13
Show Gist options
  • Save spedru/401a1fa3a52b54a13a49 to your computer and use it in GitHub Desktop.
Save spedru/401a1fa3a52b54a13a49 to your computer and use it in GitHub Desktop.
CS assignment; really ugly
#include <stdio.h>
#include <string.h>
int main(void)
{
char strings[2][255], halves[2][127];
size_t len[2];
for(int i = 0; i < 2; i++)
{
printf("Enter string %d: ", i + 1);
fgets(strings[i], 255, stdin);
strtok(strings[i], "\n");
len[i] = strlen(strings[i]);
strncpy(halves[i], strings[i] + i * len[i] / 2, len[i] / 2 + i);
halves[i][len[i] / 2 + i] = 0;
}
printf("String 1 is %zu characters long, and String 2 is %zu characters "
"long\n", len[0], len[1]);
printf("First half of string 1 and Second half of string 2 is: "
"\"%s%s\"\n", halves[0], halves[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment