function splitEvenLengthWords

The function/method splitEvenLengthWords accepts an argument str that represents a string value containing multiple words.

The function/method splitEvenLengthWords must split each even length word into two equal halves in the given string. Then the function must return a new string indicating the resulting string.

Your task is to implement the function splitEvenLengthWords so that the program runs successfully.

IMPORTANT: Do not write the main() function as it is already defined.

Example Input/Output 1:
Input:
Monday January Leapyear calendar day

Output:
String: Mon day January Leap year cale ndar day

Explanation:
Here the given string is MondayJanuaryLeapyearcalendarday.
The even length words in the given string are MondayLeapyear and calendar.
After dividing the even length words into two equal halves, the string becomes
Mon day January Leap year cale ndar day

Example Input/Output 2:
Input:
one two three four five

Output:
String: one two three fo ur fi ve

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char* splitEvenLengthWords(char *str)
{
    char* revised=malloc(sizeof(char)*1000001);
    char* tok=strtok(str," ");
    while(tok!=NULL)
    {
        if(strlen(tok)%2==0)
        {
            char temp[1001];
            int l=0;
            for(int i=0;i<strlen(tok);i++)
            {
                if(i==strlen(tok)/2)
                    temp[l++]=' ';
                temp[l++]=tok[i];
            }
            temp[l]='';
            strcat(revised,temp);
            strcat(revised," ");
        }
        else
        {
            strcat(revised,tok);
            strcat(revised," ");
        }
        tok = strtok(NULL," ");
    }
    return revised;
}
int main()
{
    char str[1001];
    scanf("%[^n]", str);
    char *revisedStr = splitEvenLengthWords(str);
    printf("String: %s", revisedStr);
    return 0;
}
Previous Article

Prefix to Postfix Conversion

Next Article

Update & Sum Queries - IntegerList

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *