Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:48:54+00:00 2026-06-05T09:48:54+00:00

I have a source code in C. #include <stdio.h> #define IN_W 1 #define OUT_W

  • 0

I have a source code in C.

#include <stdio.h>
#define IN_W 1
#define OUT_W 0
#define SPACE 32
#define TAB 9

int main() { 
    int c, state, temp;
    state = OUT_W;

    while ((c = getchar()) != EOF) {
        if ((c != SPACE || c != TAB) && (state == OUT_W)) {
            state = IN_W;
            temp = c;
            c = 13;
            putchar(c);
            c = 10;
            putchar(c);
            putchar(temp);          
        } else if (c != SPACE || c != TAB)
            putchar(c);     
        else 
            state = OUT_W;      
    }   
    return 0;
}

What I want to achieve is I will type in some characters/words and catch those inputs by getchar. When ever getchar receive any characters besides space or tab, it will print a new line and then print those characters until it found a space or tab (abandon them). For example, when I type

123  eat    4bananas     in themorning

the program will print

123
eat
4bananas
in
themorning

I tried to integrate it with CR or LF, but it still print “123 eat 4bananas in themorning”.

My questions are:
1. What did I miss?
2. In the last ‘else’, which one is more efficient for the running program:

    else 
        state = OUT_W;

or

    else if ((c == SPACE || c == TAB) && state == IN_W)
        state = OUT_W;
    else
        continue;        // or can I use single ';' since we do nothing in here?

That’s all. Thank you for your help.

Note: I tried playing with ‘\n’ and ‘\t’ too.

Regards,
Mario

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:48:57+00:00Added an answer on June 5, 2026 at 9:48 am

    For a start:

    (c != SPACE || c != TAB)
    

    is always true. A character cannot be both space and tab at the same time, hence it must always be either a non-tab or non-space. I suspect you meant:

    (c != SPACE && c != TAB)
    

    That’s why the state is never going back to OUT_W, because after the first line end sequence, the second if statement is always true, so it will never get to that final else bit.

    The following code works okay:

    #include <stdio.h>
    #define IN_W 1
    #define OUT_W 0
    #define SPACE 32
    #define TAB 9
    
    int main (void) {
        int c, state, temp;
        state = OUT_W;
    
        while ((c = getchar()) != EOF) {
            if ((c != SPACE && c != TAB) && (state == OUT_W)) {
                state = IN_W;
                temp = c;
                c = 13;
                putchar(c);
                c = 10;
                putchar(c);
                putchar(temp);
            } else if (c != SPACE && c != TAB)
                putchar(c);
            else
                state = OUT_W;
        }
        return 0;
    }
    

    although it still has that annoying initial newline, which you can fix by simply setting the initial state to IN_W.

    There’s also a lot of magic numbers in your code and some rather unnecessary moving of values. Possibly a more polished version would be:

    #include <stdio.h>
    
    #define IN_W 1
    #define OUT_W 0
    
    #define SPACE ' '
    #define TAB '\t'
    #define CR '\r'
    #define LF '\n'
    
    int main (void) {
        int c, state;
    
        state = IN_W;
        while ((c = getchar()) != EOF) {
            if ((c != SPACE) && (c != TAB) && (state == OUT_W)) {
                putchar(CR);
                putchar(LF);
                putchar(c);
                state = IN_W;
            } else if ((c != SPACE) && (c != TAB))
                putchar(c);
            else
                state = OUT_W;
        }
    
        return 0;
    }
    

    One thing I will mention is that it’s often preferable to separate the state machine itself from the actions carried out. To that end, I would make the primary choice based on the current state rather than the character/state pair, and separate the actions for each state from the state machine.

    I think that makes things a lot more readable, and easier to modify:

    #include <stdio.h>
    
    enum tState { ST_WORD, ST_SPACE };
    
    static enum tState doWord (int ch) {
        if ((ch == ' ') || (ch == '\t')) {
            putchar ('\r');
            putchar ('\n');
            return ST_SPACE;
        }
        putchar (ch);
        return ST_WORD;
    }
    
    static enum tState doSpace (int ch) {
        if ((ch == ' ') || (ch == '\t'))
            return ST_SPACE;
        putchar (ch);
        return ST_WORD;
    }
    
    int main (void) {
        int ch;
        enum tState state = ST_WORD;
    
        while ((ch = getchar()) != EOF) {
            switch (state) {
                case ST_WORD:  state = doWord  (ch); break;
                case ST_SPACE: state = doSpace (ch); break;
            }
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about the source-code binary on Windows. #include <stdio.h> int main()
i have found following code on online for suffix tree #include <stdio.h> #define E
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
I have a source code that is needed to be converted by creating classes,
I have some source code to get the file name of an url for
I have this source code from 2001 that I would like to compile. It
I have standard source code package under Linux which requires to run ./configure make
I have this source code: idx=0 b=plt.psd(dOD[:,idx],Fs=self.fs,NFFT=512) B=np.zeros((2*len(self.Chan),len(b[0]))) B[idx,:]=20*log10(b[0]) c=plt.psd(dOD_filt[:,idx],Fs=self.fs,NFFT=512) C=np.zeros((2*len(self.Chan),len(b[0]))) C[idx,:]=20*log10(c[0]) for idx
I have java source code in a text file. There has to be entered
I have an installer for which I don't have any source code. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.