Saif Khaled Saif Khaled - 18 days ago 5
C Question

Can anyone help me how to creat a c program prints the center of the words inside a sentance?

Example:
My name is ray => ( a )

Another example :
Hello the world. =>. ( l h r )

Answer

Here's my solution:

#include <stdio.h>

int main()
{    
  char sentence [100];
  int i,letter_count=0;

  printf("Enter a sentence: ");
  fgets(sentence,100,stdin);

  for(i=0;sentence [i]!='\0';i++)
  {
    if(sentence [i]==' ' || sentence [i+1]=='\0')
    {
      if(letter_count%2==1)
      {
        printf("%c ",sentence [i-letter_count/2-1]);
      }
      letter_count=0;
    }
    else
    {
      letter_count++;
    }
  }
  putchar('\n');
  return 0;
}