Advertisement
Guest User

text line string

a guest
May 29th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. /*  text_line_string()
  2.  *  Generate and return a string, as above, with text centered.
  3.  */
  4. char* text_line_string(char *text, int length, char first, char second) {
  5.   int text_length, text_print_length, pre_length;
  6.   int i = 0, j = 0;
  7.   static char buf[MAX_STRING_LENGTH]; /* Note - static! */
  8.  
  9.   text_length = strlen(text);
  10.   text_print_length = count_non_protocol_chars(text);
  11.  
  12.   pre_length = (length - (text_print_length))/2; /* (length - (text length + '[  ]'))/2 */
  13. //  pre_length = (length - (text_length + 4))/2; /* (length - (text length + '[  ]'))/2 */
  14. //  pre_length = 2;
  15.  
  16.   while (i < pre_length) {
  17.     if((i % 2) == 0)
  18.       buf[i] = first;
  19.     else
  20.       buf[i] = second;
  21.     i++;
  22.   }
  23. //  buf[i++] = '[';
  24. //  buf[i++] = ' ';
  25.  
  26.   while (j < text_length)
  27.     buf[i++] = text[j++];
  28.  
  29. //  buf[i++] = ' ';
  30. //  buf[i++] = ']';
  31.  
  32.   while (i < length + (text_length - text_print_length)) /* Have to include the non printables */
  33.     if((i % 2) == 0)
  34.       buf[i++] = first;
  35.     else
  36.       buf[i++] = second;
  37.   buf[i++] = '\r';
  38.   buf[i++] = '\n';
  39.   buf[i]   = '\0';    /* Terminate the string. */
  40.  
  41.   return buf;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement