Guest User

Untitled

a guest
May 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SPACES 4
  4. #define SLIMIT 1000
  5.  
  6. int getlinec(char line[], int lim);
  7. void spacetotab(char result[], char line[]);
  8. /*
  9. Replaces strings of blanks by the minimum number of tabs and blanks
  10. */
  11. int main()
  12. {
  13. char line[SLIMIT];
  14. char result[SLIMIT];
  15. while (getlinec(line, SLIMIT) > 0) {
  16. spacetotab(result, line);
  17. printf("%s\n", result);
  18. }
  19.  
  20. return 0;
  21. }
  22.  
  23. /*
  24. gets input from stdin and stores it in a string (null-terminated and '\n'ed)
  25. */
  26. int getlinec(char line[], int lim)
  27. {
  28. int i, c;
  29.  
  30. for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
  31. line[i] = c;
  32. if(line[i] == '\n') {
  33. line[i] = '\n';
  34. i++;
  35. }
  36. line[i] = '\0';
  37.  
  38. return i;
  39. }
  40.  
  41. /*
  42. turns each four spaces to a tab in a string(not '\n'ed)
  43. */
  44. void spacetotab(char result[], char line[])
  45. {
  46. int i, j;
  47. int spaces = 0; // count spaces
  48.  
  49. i = j = 0;
  50. while (line[i] != '\0') {
  51. if(line[i] == ' ') {
  52. spaces++;
  53. i++;
  54. } else {
  55. result[j] = line[i];
  56. i++;
  57. j++;
  58. }
  59. if(spaces == 4) {
  60. result[j] = '\t';
  61. spaces = 0;
  62. j++;
  63. }
  64. }
  65. result[j] = '\0'; // null terminate
  66. }
Add Comment
Please, Sign In to add comment