#include <stdio.h>
#include <stdlib.h>

void main()
{
    char s[40];
    int i,j,k=0,c=0;
    printf("enter the string: ");
    fgets(s,sizeof(s),stdin);
    for(i=0;s[i]!='\0';i++)
    {
        if(s[i]==32)
        {
            c++;
        }
    }
    if(c%2==0)
    {
        printf("there is no middle word");
    }
    else{
        j=c/2;

        for(i=0;s[i]!='\0';i++)
        {
            if(s[i]==32)
            {
                k++;
                if(j==k)
                {
                    i++;
                    while(s[i]!=32)
                    {
                        printf("%c",s[i]);
                        i++;
                    }
                }
            }
        }
    }


}

output shoulbe like this
i love india
where middle word is love
so i have to print middle word
it shows 0 errors but output is wrong

use the strtok function and set space as the delimeter and save each to an array (use a 2d array in this case), count the number of tokens and divide it by 2 then print out the word with the same value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.