Question

Write a C program named space_to_line.c that features a while loop that continuously reads input from...

Write a C program named space_to_line.c that features a while loop that continuously reads input from the user one character at a time and then prints that character out. The exception is that if the user inputs a space character (‘ ‘), then a newline character (‘\n’) should be printed instead. This will format the output such that every word the user inputs is on its own line. Other than changing the spaces to newlines, the output should exactly match what the user inputs. If the user inputs a period character (‘.’) then the loop should break and the program should terminate. You should also print a single newline character after the loop breaks, but before the program exits. This just ensures that the shell prompt will be on a new line after the program finishes. For this problem, you should use getchar() to read input and putchar() to print input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <conio.h>
int main()
{
char c;
while(1) {
c = getchar();
if (c == '.'){ //Break the loop if user enters '.'
break;
}
else if(c == ' ') { //Check if user entered space, print '\n' instead of space
putchar('\n');
}
else {
putchar(c);
}
}
putchar('\n'); // Single newline character before exiting the program
return 0;
}

Explanation are in the code as comments, Feel free to reach out if you have any doubts.

Add a comment
Know the answer?
Add Answer to:
Write a C program named space_to_line.c that features a while loop that continuously reads input from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that will loop ten times. In each iteration prompt user to enter an...

    Write a program that will loop ten times. In each iteration prompt user to enter an integer between -50&50. Print the input, keep a running sum of the inputs, and track the minimun and maximum numbers input. After the loop is complete, minimum number entered, maximum number entered, sum of all numberes entered, and average of all numbers entered. Format all output. Before running the loop, print the label "Input values: ". Within the loop, print value entered by user...

  • please I need help with the question: Problem: Write a C++ program that reads each character...

    please I need help with the question: Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits. (*The main difference between “cin >>...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • USE of C++ please Write a program that achieves the following requirements: 1. A while/do-While loop...

    USE of C++ please Write a program that achieves the following requirements: 1. A while/do-While loop that will ask the user to input an integer on each iteration. The loop keeps a running total of the input and exits only when the total exceeds the first input times 10. 2. A for loop that does the exact same thing. The program should print out the *new* total each time the total is input.

  • (50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands...

    (50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands and perform the corresponding calculation on the operands. Specific requirements: (1) Your main function accepts inputs and display calculation result. (2) Write four functions to perform the four calculations. Call these functions in your main function. (3) Repeatedly asks for inputs, calculate and display result until user input nonnumeric value for operands. Hint: you can put getchar() right after scanf() to get rid of...

  • C++ Program Write a do-while loop that continues to prompt a user to enter a number...

    C++ Program Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with a newline. Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 #include <iostream> using namespace std; int main() { int userInput; /* Your solution goes here */...

  • Write a program that takes in a line of text as input, and outputs that line...

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH уен Hint: Use the fgets function to read a string with spaces from the user...

  • 4) (12 points) Write the CH code that continuously (use a loop) prompts a user to...

    4) (12 points) Write the CH code that continuously (use a loop) prompts a user to input a floating point number. Each time, the code prints out the input, numbered. So if the user inputs a -2.3 for the first input and a 116.79 for the second input, the output is input 1: -2.3 input 2: 116.79 The loop keeps running until the sum of all the numbers input is greater than 100. (This needs an accumulator!)

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Write a Java program that reads in a word from the user and outputs each character...

    Write a Java program that reads in a word from the user and outputs each character of the word on a separate line. For example, if the user enters the input "Joe", your program should output: J o e You need to use a for-loop.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT