C Language K&r Exercise series-Four ways to replace multiple spaces in a single space in a sentence

Source: Internet
Author: User

Original question:

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.


The first type:

This is most commonly used to set a inspace as a Boolean variable that flags whether the current input is in a character, or outside of a character


#include <stdio.h>  int main (void) {  int c;  int inspace=0;    while ((c = GetChar ()) = EOF)  {    if (c = = ")    {      if (InSpace = = 0)      {        inspace = 1;        Putchar (c);      }    }    else    {      inspace = 0;      Putchar (c);    }  }    return 0;}


The second kind: own thought, although the effect is similar, but the mentality is different

Number as the flag bit, record the amount of space



#include < stdio.h > main (void) {    int c;    int number;    Number = 0;       Initialization space Numbers           while ((c = GetChar ()) = EOF)    {        if (c = = ')        {            ++number;            if (number = = 1)                Putchar (c);        }        if (c! = ")        {            Putchar (c);            Number = 0;        }    }    return 0; }


The third type:

Use a while loop to "take" multiple spaces


#include < stdio.h >main (void) {    int c;    while ((c = GetChar ()) = EOF)    {        if (' = = c)        {            Putchar (c);            while ((c = GetChar ()) = = "&& c! = EOF)                ;        }         if (EOF = = c) break            ;         Putchar (c);    }    return 0;}


The fourth type:

Use a character pc as the character before the "current character" to determine whether the trailing space is output by the PC


#include <stdio.h>int main () {        int c, PC;           PC = EOF;          while ((c = GetChar ()) = EOF) {                if (c = = ")                        if (PC! =")                                   Putchar (c);                  if (c! = ")                        Putchar (c);                PC = C;        }          return 0;}



C Language K&r Exercise series-Four ways to replace multiple spaces with a single space in a sentence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.