#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#define MAXLEN        (200+1)

void phrase_rev( char []);
void phrase_fill( char []);

int main( void )
{
        char input[ MAXLEN ] = {'\0'};
 
        printf(" Give a sentence: ");
        
        phrase_fill(input);
        phrase_rev(input);
        
       return 0;
}
void phrase_fill( char input[])
{
    int c ;
    int i ;
   
    for (i=0; i < MAXLEN-1 && '\n' != (c = getchar()) && c != '.' && c != '?'; i++) {
		if( ispunct(c)) {
			printf("\n Character %c ignored " , c);
			--i;
			continue;
		}
		input[i] = c;
	}
                
        input[i] = '\0';
        
        return ;
}
	
void phrase_rev( char input[])
{
	int     i ;
    bool    onspace = false;
   //int c;

        printf(" The reversal of the sentence: ");
           
        for (i= MAXLEN-1 ; i > -1; i--) {
        
                if ( isspace(input[i]) && !onspace ) {          /* Σε σημείο κενού */
                        printf( "%s%c", &input[i+1] , input[i]);
                        input[i] = '\0';
                        onspace = true;
                }
                else if ( isspace( input[i] ) )                 /* space-area has >1 chars*/
                       putchar( input[i] );                     /* For two consecutive spaces*/
                else                                            /* Σε περιοχη που δεν εχει κενό   */
                        onspace = false; 
        }
        printf("%s" , input );            // printf αντι της puts επειδη η τελευταία προσαρτά και έναν χαρακτήρα αλλαγής γραμμής.
       //putchar(c);
        
	return ;
}
//----------------------------------------------------------------------------------------------
