# I need help with this string problem

 Paul Kim · February 7, 2016 Problem 1:Write a program that reads a string from the keyboard. If the length of the string is an even number, your program should split the string into two strings of equal length. If the length of the string is odd, your program should split the string into two strings where the first part has one more character than the second part. Your program should output the two strings it created. For example: Input: 2BeOrNotToBeOutput: 2BeOrNotToBeInput: ToBeOrNotToBeOutput: ToBeOrN otToBe#include #include #include int main(){ char str[1000]; // Input string to be saved in str. Also assuming max length og input string can be 1000 printf("Input : "); int i,j,counter=0; scanf("%s",str);// saves the input string into str for(i=0; str!='\0'; ++i); // loop to calculate the length of string str int length=i; // length is saved in 'length' variable char newStr2[1000]; // the second new string will be saved in newStr string // whereas the first new string after splitting will be saved in 'str' string if(length%2==0) // if the length is even { for(i=length/2;i

## Replies

 kuldeep kanzariya · February 16, 2016 #include #include #include int main(){        char str[1000]; // Input string to be saved in str. Also assuming max length og input string can be 1000        printf("Input : ");        int i,j,counter=0,length=0;        scanf("%s",str);// saves the input string into str        for(i=0; str!='\0'; ++i) // loop to calculate the length of string str        length=i+1; // length is saved in 'length' variable        char newStr2[1000]; // the second new string will be saved in newStr string        // whereas the first new string after splitting will be saved in 'str' string        if(length%2==0) // if the length is even        {                for(i=length/2;iint main(int argc, char **argv) { if (argc <= 1) { puts("usage: ./app \"string\\0\""); return 0; } size_t length = strlen(argv[1]), y_length = length / 2, x_length = y_length + length % 2; char x[x_length + 1], y[y_length + 1]; snprintf(x, sizeof x, "%s", argv[1]); sprintf (y, "%s", argv[1] + x_length); printf("%s\n%s\n", x, y);}``There's a bit of room for optimisation here; we don't really need to create a string just to print that output. It's a bit of a premature optimisation, but the less memory we use and the less string copying we perform the more likely it is to perform well, so...``#include int main(int argc, char **argv) { if (argc <= 1) { puts("usage: ./app \"string\\0\""); return 0; } size_t length = strlen(argv[1]), y_length = length / 2, x_length = y_length + length % 2; fwrite(argv[1], 1, x_length, stdout); putchar('\n'); fwrite(argv[1] + x_length, 1, y_length, stdout); putchar('\n');}``I can't recommend doing any of these exercises as they don't really have a point... If you want to learn programming, consider the exercises in K&R... If you want to learn program design, on the other hand, I can recommend project euler. This kind of exercise, however, falls unfortunately short for both.
• 1

## C

129,724 followers