-1
#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

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by zeroliken
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.