fork download
  1. #include <stdio.h>
  2. // INPUT: abcd a abcdef a
  3. // OUTPUT: abcd****a******abcdef*a [
  4. // OUTPUT: ---Detab: abcd****ba*****abcdef*<a
  5. #define MAXLINE 1000
  6.  
  7. void detab (char s [], int tabwidth, int lim);
  8. int main ()
  9. {
  10. int i, c, max;
  11. max = MAXLINE;
  12. char input [MAXLINE];
  13. for (i = 0; i < max && (c = getchar ()) != EOF; ++i)
  14. {
  15. input [i] = c;
  16. }
  17. detab (input, 8, max);
  18. printf ("\n---Detab: %s...\n", input);
  19. return 0;
  20. }
  21.  
  22. // lim must be larger than true size to allow for conversion and extra chars
  23. void detab (char s [], int tabwidth, int lim)
  24. {
  25. int i;
  26. int j = 0;
  27. char t [lim];
  28. for (i = 0; i < lim && s[i-1] != '\0'; ++i)
  29. {
  30. t[i] = s[i];
  31. }
  32. t [i] = '\0';
  33. // Good through here
  34. // t never changes from here out
  35.  
  36.  
  37. for (i = 0; t[i] != '\0' && i < lim; ++i)
  38. {
  39. putchar (t [i]);
  40. // Work on this
  41. // Problem is that the first tab is always correct
  42. // remaining tabs have 1 less '*' than necessary and the next is missing 2. etc.
  43. // also returning the string leads to dirty end of string for some reason
  44. // as in weird letters appended
  45. if (t [i] == '\t')
  46. {
  47. while ((j % tabwidth) != 0)
  48. {
  49. s [j] = '*';
  50. //putchar (s[j]);
  51. ++j;
  52. }
  53. //i++ maybe
  54. --j;
  55. }
  56.  
  57. else
  58. {
  59. s[j] = t[i];
  60. //putchar (s[j]);
  61. }
  62. ++j;
  63. }
  64. // last char of s is null
  65. s [j+1] = '\0';
  66. }
Success #stdin #stdout 0.01s 5284KB
stdin
ABCD\t
stdout
---Detab: A...