9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
guia03/ex16.c (552B)
   1 /* Reads a string from stdin and if it ends in blank
   2  * spaces it erase them and move the '\0' char
   3  * it performs a "right trim"
   4 */
   5 
   6 #include <stdio.h>
   7 
   8 #define MAX_LEN 100
   9 
  10 int
  11 main (void)
  12 {
  13     int i, j, k;
  14     char str[MAX_LEN];
  15 
  16     if(!fgets(str, MAX_LEN, stdin))
  17         return 1;
  18 
  19     /* Goes to end of string, then starts counting trailing spaces */
  20     for(i = 0; str[i]; i++) 
  21         if(str[i + 1] == '\n') 
  22             for(j = 0; str[i - j] == 32; j++)
  23                 ;
  24     
  25     /* Places the '\0' character j times 'til end of string    */
  26     str[i - j] = '\0';
  27 
  28     printf("%s", str);
  29     return 0;
  30 }