I'm trying to get this program to ignore leading tabs and blanks, but I just can't seem to figure out how. What can I further do to improve this program?

/* Read a command, skipping over leading blanks and any trailing
 * characters.
*/

#include <stdio.h>

/* Declare functions */
int skipBlanks(void);
int skipOverRestOfCommand(void);

int main()
{
  /* Variable declarations */
  int cmd;

        /* cmd is blank space */
        cmd = skipBlanks();
        /* While cmd is not equal to end of file */
        while (cmd != EOF)
        {
                printf("The command is: %c\n", cmd);
                skipOverRestOfCommand();
                cmd = skipBlanks();

        }
} /* End main */

/* Function has no parameters */
int skipBlanks(void)
{
  /* Variable declarations */
  int c;

        /* Return character from standard input */
        c = getchar();

        /* While c is equal to a blank space */
        while (c == ' ')
        {
                /* Return char from standard input */
                c = getchar();
        }


        /* Return c */
        return c;
}
/* Function has no parameteres */
int skipOverRestOfCommand(void)
{
  /* Variable declarations */
  int c;

        /* Return char as int */
        c = getchar();

        /* While c is a new line */
        while (c != '\n')
        {
                /* Return char as int */
                c = getchar();
        }
        /* Return c */
        return c;
}

Recommended Answers

All 5 Replies

A simple while loop before the char's you want, is all you need.

i = 0;
while(str[i] < 'A')
  ++i;

//get your good input here

//then ignore the rest of the char's.

A simple while loop before the char's you want, is all you need.

i = 0;
while(str[i] < 'A')
  ++i;

//get your good input here

//then ignore the rest of the char's.

would this go into the first function skipBlanks?
I'm not really proficient in C++ so how would this look like in regular C code?

That IS regular C code - I don't code in C++.

I don't believe it needs it's own function for 3 lines of code. You can do that, of course. I tend to group functions by their functionality. Here, you're doing input - and this is a small part of doing that, so I'd put it together with the rest of the input code.

would it look something like this?

int main()
{
  /* Variable declarations */
  int cmd;
  int i=0;

        /* cmd is blank space */
        cmd = skipBlanks();
        /* While cmd is not equal to end of file */
        while (cmd != EOF)
        {
                while(str[i] < 'A')
                {
                  ++i;
                printf("The command is: %c;\n", cmd);
                skipOverRestOfCommand();
                cmd = skipBlanks();
                }

        }
} /* End main */

If you need to get the command, why do you skipOverRestOfCommand()? Why are you throwing the command away? Save it and returnTheCommand() instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.