Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SPACES 4
- #define SLIMIT 1000
- int getlinec(char line[], int lim);
- void spacetotab(char result[], char line[]);
- /*
- Replaces strings of blanks by the minimum number of tabs and blanks
- */
- int main()
- {
- char line[SLIMIT];
- char result[SLIMIT];
- while (getlinec(line, SLIMIT) > 0) {
- spacetotab(result, line);
- printf("%s\n", result);
- }
- return 0;
- }
- /*
- gets input from stdin and stores it in a string (null-terminated and '\n'ed)
- */
- int getlinec(char line[], int lim)
- {
- int i, c;
- for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
- line[i] = c;
- if(line[i] == '\n') {
- line[i] = '\n';
- i++;
- }
- line[i] = '\0';
- return i;
- }
- /*
- turns each four spaces to a tab in a string(not '\n'ed)
- */
- void spacetotab(char result[], char line[])
- {
- int i, j;
- int spaces = 0; // count spaces
- i = j = 0;
- while (line[i] != '\0') {
- if(line[i] == ' ') {
- spaces++;
- i++;
- } else {
- result[j] = line[i];
- i++;
- j++;
- }
- if(spaces == 4) {
- result[j] = '\t';
- spaces = 0;
- j++;
- }
- }
- result[j] = '\0'; // null terminate
- }
Add Comment
Please, Sign In to add comment