Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a basic C program that will read standard input and, where possible, repla

ID: 3568307 • Letter: W

Question

Write a basic C program that will read standard input and, where possible, replace each sequence of blank spaces with one or more tab characters and then echo the modified input to standard output. You may assume that tab stops occur at columns 1, 9, 17, 25, and so on. The output of the progrom should appear identical to the input even though the characters might not be the same.

This program would be used on a system that allowed input and output redirection. it would produce text that looked identical to the input text but contained fewer characters and therefore took up less memory or disk space. Running the program at a terminal without redirection would alternate lines of input and output.

Explanation / Answer

#include #include #include #define SPACE ' ' int main(){ char string[100], *start, pv; int c, d; printf("Enter a string "); scanf("%99[^ ]", string); start = malloc(strlen(string)+1); if( start == NULL ) exit(EXIT_FAILURE); pv = 0;//previous character for(d=c=0; string[c]; ++c){ if(string[c] == SPACE){ if(pv != SPACE) start[d++] = '%'; pv = SPACE; } else { pv = start[d++] = string[c]; } } start[d] = ''; printf("%s ", start); free(start); system("PAUSE"); return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote