training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

justify.c (1085B)


      1 /* Exercise 8.35 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #define SIZE 30
      8 #define BUF  255
      9 
     10 #define C4L 65 /* Characters for line */
     11 
     12 int main(void)
     13 {
     14    int i = 0, j;
     15    char string[SIZE][BUF] = { { 0 } };
     16 
     17    printf("Give me a string (%d to end): ", EOF);
     18    gets(string[i]);
     19    while( atoi(string[i++]) != EOF && i < SIZE ) {
     20       printf("Give me a string (%d to end): ", EOF);
     21       gets(string[i]);
     22    }
     23    string[i - 1][0] = '\0'; /* remove the EOF */
     24 
     25    /* loop the strings */
     26    for(i = 0; i < SIZE && strlen(string[i]); i++) {
     27 
     28       /* print the current line formatted */
     29       printf("%10c", ' '); /* Margin left */
     30       for(j = 1; j <= BUF && j <= (int)strlen(string[i]); j++) {
     31 	 printf("%c", string[i][j - 1]);
     32 
     33 	 if( !(j % C4L) ) { /* Characters for line */
     34             printf("\n%10c", ' '); /* Margin left */
     35 
     36 	    /* Toggle the spaces and the newlines */
     37 	    if(string[i][j] == ' ' || string[i][j + 1] == '\n')
     38 	       ++j;
     39 	 }
     40       }
     41       printf("%10c\n", ' '); /* Margin right */
     42 
     43    } /* end for (i) */
     44 
     45    return 0;
     46 } /* E0F main */
     47