Ask Question
24 September, 23:24

Write a basic C 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, if the input to the program is "2BeOrNotToBe", the output of the program should be

a. 2BeOrN

b. otToBo

If the input is "ToBeOrNotToBe", the output of the program should be

a. ToBeOrN

b. otToBe

+4
Answers (1)
  1. 25 September, 02:29
    0
    Answer: Here is the C program:

    #include

    #include

    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 / n"); / /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] = = '/n') / /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/n", substring1); / /displays first substring

    printf ("b. %s/n", substring2); } / /displays second substring

    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.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a basic C program that reads a string from the keyboard. If the length of the string is an even number, your program should split the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers