Showing posts with label Equal First and Second Half. Show all posts
Showing posts with label Equal First and Second Half. Show all posts

Saturday 9 February 2019

Equal First and Second Half String

The program must accept a string S as the input. The program must print yes if the first half of the string is equal to the second half of the string. Else the program must print no as the output.
Note: If the length of the string is odd ignore the character in the middle.

Boundary Condition(s):
1 <= Length of the string <= 1000

Input Format:
The first line contains the string S.

Output Format:
The first line contains either yes or no.

Example Input/Output 1:
Input:
dumdum
Output:
yes
Explanation:
Both the first and second half of the string is same so yes is printed.

Example Input/Output 2:
Input:
YelYe
Output:
yes

Program
#include <stdio.h>

int main()
{
    char S[1000];
    int i,j=0,a;
    scanf("%s",S);
    a=strlen(S);
    if(a%2==0)
       j=a/2;
    else
       j=a/2+1;
    for(i=0;i<a/2;i++)
    {
        if(S[i]!=S[j++])
        {
            printf("no");
            break;
        }
    }    
    if(i==a/2)
        printf("yes");
            
}


Output:
tellet
no