fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #define MAXLEN (200+1)
  6.  
  7. void phrase_rev( char []);
  8. void phrase_fill( char []);
  9.  
  10. int main( void )
  11. {
  12. char input[ MAXLEN ] = {'\0'};
  13.  
  14. printf(" Give a sentence: ");
  15.  
  16. phrase_fill(input);
  17. phrase_rev(input);
  18.  
  19. return 0;
  20. }
  21. void phrase_fill( char input[])
  22. {
  23. int c ;
  24. int i ;
  25.  
  26. for (i=0; i < MAXLEN-1 && '\n' != (c = getchar()) && c != '.' && c != '?'; i++) {
  27. if( ispunct(c)) {
  28. printf("\n Character %c ignored " , c);
  29. --i;
  30. continue;
  31. }
  32. input[i] = c;
  33. }
  34.  
  35. input[i] = '\0';
  36.  
  37. return ;
  38. }
  39.  
  40. void phrase_rev( char input[])
  41. {
  42. int i ;
  43. bool onspace = false;
  44. //int c;
  45.  
  46. printf(" The reversal of the sentence: ");
  47.  
  48. for (i= MAXLEN-1 ; i > -1; i--) {
  49.  
  50. if ( isspace(input[i]) && !onspace ) { /* Σε σημείο κενού */
  51. printf( "%s%c", &input[i+1] , input[i]);
  52. input[i] = '\0';
  53. onspace = true;
  54. }
  55. else if ( isspace( input[i] ) ) /* space-area has >1 chars*/
  56. putchar( input[i] ); /* For two consecutive spaces*/
  57. else /* Σε περιοχη που δεν εχει κενό */
  58. onspace = false;
  59. }
  60. printf("%s" , input ); // printf αντι της puts επειδη η τελευταία προσαρτά και έναν χαρακτήρα αλλαγής γραμμής.
  61. //putchar(c);
  62.  
  63. return ;
  64. }
  65. //----------------------------------------------------------------------------------------------
  66.  
Success #stdin #stdout 0.01s 1680KB
stdin
Standard input is empty
stdout
 Give a sentence:  The reversal of the sentence: ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������