Answer: Here is the C program:
#include <stdio.h>
#include <string.h>
int main(){
int max; //to store the maximum length of the input string
char userString[max]; //the string entered from the keyboard
char substring1[max]; // the first part of the input string after split
char substring2[max]; // the second part of input string after split
int len, splitstr;
//len stores total length of the input string and splitstr stores length of split
printf("Enter a string \\"); //prompts user to enter a string
fgets(userString, sizeof(userString), stdin);
// reads the string entered by user and saves it in userString
len = strlen(userString); //stores length of the input string
if(userString[len-1] == '\\') //checks if end of the input string is reached
{ userString[len-1] = '\0';
len--; }
if(len % 2) //to check if the length of entered string is even or odd
splitstr = len/2 + 1; // if its odd then divide the input string into half+1
else
splitstr = len/2; //if its even divide the input into two equal halves
strncpy(substring1, userString, splitstr);
//copies characters from user string to substring1
substring1[splitstr] = '\0';
strcpy(substring2, userString + splitstr);
//copies characters from user string to substring2
printf("a. %s\\", substring1); //displays first substring
printf("b. %s\\", substring2); } //displays second substring
Step-by-step explanation:
The program prompts the user to enter a string.
fgets function is used to read the string entered from keyboard and store it into userString character type variable.
len is used to store the length of the string entered by the user.
If the length of the input string i.e. number of characters in the input string is odd then the input string is divided into two sub strings in such a way that the number of characters in the first substring are one more than the number of characters of the second substring. % operator is used to identify if the input string is even or odd.
If the length of the input string is even then the input string is divided into two equal halves.
strncpy function is used to store the characters of the userString to the substring1 according to the number of characters given in the splitstr. This means that the first split will be stored in substring1.
strcpy function is also used to store the second split in substring2 variable.