C Program to Divide a String into Two Equal Strings

Here is a C program to divide a string in two equal sub strings. Given a string of length L, we have to split this string into two equal sub-strings.

  • If L is even, then length of the sub-strings will be L/2 and L/2.
  • If L is off, the length of sub-strings will be L/2 and (L/2)+1
For Example:
Input : "Internet" Output : "Inte" and "rnet"
Algorithm for split a string in two equal sub strings.
Let inputString be the string entered by user and leftHalf and rightHalf are two output sub-strings.
  1. Find length of the string using strlen function. Let it be L.
  2. Find the mid index of input string. (mid = L/2)
  3. Copy characters of inputString from index 0 to mid to leftHalf.
  4. Copy characters of inputString from index mid+1 to L-1 to rightHalf.

C program to split a string into two equal strings

In this program, we will first read a string as input from user using gets function. Then we find the length of input string(L) using strlen function of string.h header file. Now, we will create two sub-strings as explained above and print it on screen.

#include<stdio.h>
#include<string.h>

int main() {

    char inputString[100], leftHalf[100], rightHalf[100];
    int length, mid, i, k;
 
    printf("Enter a string\n");
    gets(inputString);

    length = strlen(inputString);
 
    mid = length/2;
    
    for(i = 0; i < mid; i++) {
        leftHalf[i]= inputString[i];
    }
    leftHalf[i] = '\0';
 
    for(i = mid, k = 0; i <= length; i++, k++) {
        rightHalf[k]= inputString[i];
    }
 
    printf("Left half : %s\n",leftHalf);
    printf("Right half : %s\n",rightHalf);

    return 0;
}
Output
Enter a string
TECHCRASHCOURSE
Left half : TECHCRA
Right half : SHCOURSE

Related Topics
C program to check if two strings are anagram
C program to remove extra spaces from string
C Program to find frequency of characters in a string
C program to sort characters of a string
C program to concatenate strings
C program to convert lowercase string to uppercase
C program to reverse a string
C program to swap two strings
C program to convert string to integer
List of all C programs