c - Spliting the value of a % between multiple characters -


i've been given assignment question, , i've managed figure out far, exception of splitting remainder value betwen few (input determinate) characters. appreciated. reference, assignment question is:

"if @ newspaper see writing justified fit columns. write program reads in width of columns in newspaper , line of text. justify line of text fit column of width. when program running, screen should this:

 enter width of column: 40 enter line of text: morning how you? 12345678901234567890123456789012345678901234567890...     morning     how        you? 

the justification done counting number of gaps in text. in above example, there 4 gaps. each gap must have spaces added it. number of spaces must shared out evenly possible. in above example, first 3 gaps have 5 spaces each , last gap has 4 spaces.

notes:

  1. if text longer column must report error – don't try , break 2 lines!
  2. assume text have more 1 word in it.
  3. note header line consisting of 123456789012345678.... useful check result. can make header line long – 70 spaces useful length.

"

and code far is:

int main() {     //input column width     printf("enter width of column: ");     int column;     scanf("%d", &column);      //input text line     printf("enter line of text: ");     char string[80];     getchar();     gets(string);      //print 1234567890 column header     int y = 1,x = 0;     while(x < column){         if(y > 9){             y = 0;         }         printf("%d", y);         y++;         x++;     }     printf("\n");      //count spaces     int = 0;     int space_count = 0;     while(string[i] != '\0'){         if(string[i] == 0x20){             space_count++;         }         //printf("%c", string[i]);         i++;     }      //work out variables     int string_length = i;     int remainder_space = (column - string_length);     int space_power = (remainder_space / space_count);     //int oddremainder = (space_count % remainder_space) ;     //space_power = (space_power + oddremainder);     //if     //remainder %      //insert column width check     if(string_length > column)     {         printf("text long. shouldn't more %dcharacters\n",             column);         return 1;     }     //output     = 0;     while(string[i] != '\0'){         if(string[i] == 0x20){             for(x = 0; x < space_power; x++){                 printf("%c", 0x20);             }         }         printf("%c", string[i]);         i++;     } 

i'm sorry if isn't appropriate way of asking question, brain fried , can't head around this. pointers or discusion in right direction appreciated.

lets see example. has 19 spaces filled in 4 gaps. if code runs that, value of space_power 4 (int(19/4)), , leaves 3 spaces in end. need keep track of 19 % 4, ie. 3 spaces.

so, keep count, equal 3. then, while count greater 0, print 1 space along space_power number of spaces. decrease count every time print word.

your code this:

count = remainder_space % space_count; 

and output block:

i = 0; while(string[i] != '\0'){     int k = count > 0 ? 1 : 0;     if(string[i] == 0x20){         for(x = 0; x < space_power + k; x++){             printf("%c", 0x20);         }         count--;     }     printf("%c", string[i]);     i++; } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -