Write a program that will read standard input and echo each line to standard out
ID: 3823439 • Letter: W
Question
Write a program that will read standard input and echo each line to standard output with a line number and tab preceding it. When you run this program and enter lines from the terminal, lines of input will be interspersed with lines of output. If your system has output redirection and you redirect output to a file, the file will look like the input with the lines numbered.
Here is an example of how the script should work.
User input in bold.
Enter your text:
This is line 1.
1 This is line 1.
This is line 2.
2 This is line 2.
This is the last line of input.
3 This is the last line of input.
The last line will end the process.
This is the C programming help me witht his
Explanation / Answer
#include <stdio.h>
int main(){
char inputText[100];
int count=1;
printf("Enter yout text: ");
for(count = 1; count <= 3; ++count)
{
inputText[0]='';
/* Read string from user using scanf store it in inputText char array */
scanf(" %[^ ]", inputText);
/* Print the number */
printf("%d ", count);
/* Print string stored in inputText using printf */
printf("%s ", inputText);
}
return 0;
}
The above program does exactly what is asked in the question. Currently I have kept the string length as 100 which can be increased depending on the inpur length.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.