Thread: Collapse spaces

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    2

    Collapse spaces

    Write a program that reads all input (from standard input) and prints it transformed as follows:

    • any sequence of whitespace that does not contain newline is replaced with a single space character
    • whitespace characters immediately before a newline are deleted.

    I'm just starting out in C and I need some helping understanding this problem. My main issue would be how do I print the modified text after making the program? If you decide to solve it, please use comments so I can understand what you're doing. Thank you!!

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    Have you attempted to write this yourself? We are not here to write the code for you.

    Make an attempt to write this yourself, then if you have questions or problems, post your code, and we can give you advice.

    Please read the Forum Rules FAQs before posting here.

    Was this an assignment from an Instructor, or from a book, or online tutorial?
    Last edited by rstanley; 10-11-2020 at 05:22 AM.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    2
    Quote Originally Posted by rstanley View Post
    Have you attempted to write this yourself? We are not here to write the code for you.

    Make an attempt to write this yourself, then if you have questions or problems, post your code, and we can give you advice.

    Please read the Forum Rules FAQs before posting here.
    I did not mean to make it look like I just want someone to do my task, I just need some help. I wrote this so far:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int collapseSpaces{
         int c1=getchar();
         int c2=getchar();
         while(isspace(c1) && issspace(c2)){   // my logic here was that I'd check if two consecutive characters are both spaces and if it is true, I'd replace them with a single space character
                 c1=c2=' ';
         }
    int main()
    {
    return 0;
    }

    I've also created a .txt file (I work in putty), in order to write some text to check if the program is working. As I stated above, I also need some help understanding how to print the modified text in main. Also I don't fully understand the newline part of the problem.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    804
    This would be my basic approach

    1) Read one char at a time from stdin
    2) If char is a space, keep reading chars until a non space char or newline is input
    3) If not a newline, print out one space, the non-space char just read, and continue
    If the char just read is a newline, then just output the newline and continue

    If the input is:
    "This is a test. [newline]"

    It means to trim the trailing spaces as well as the spaces between words. The results should be"
    "This is a test.[newline]"

    I would use loops instead of collapseSpaces().

    Start simple, one step at a time

    Read one char at a time and output it to the stdout using putchar(), ignoring multiple spaces.

    Then add the check for multiple spaces.

    If input is directly from a keyboard, instruct the user to use [Ctrl] [d] on a Linux system, or [Ctrl] [z] on a Windows computer to terminate the program, or some specific char (Symbol?).

    One alternative is to write the chars out to a disk file.
    1) open the disk file
    2) Write the chars out to the disk file using fputc()
    3) Close the disk file
    Last edited by rstanley; 10-11-2020 at 06:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I allow spaces on a cin?
    By boblablabla in forum C++ Programming
    Replies: 10
    Last Post: 12-09-2009, 02:05 PM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. Getting rid of spaces in C
    By Poincare in forum C Programming
    Replies: 2
    Last Post: 01-23-2009, 03:08 PM
  4. set spaces in Tab.
    By nilrac in forum C Programming
    Replies: 8
    Last Post: 11-14-2002, 01:58 PM

Tags for this Thread