Removing a space from the start of a array to use strcmp

Viewed 361

Hello I am doing a looping on my code with fgets and I want that when user introduces the word "bye" the program ends, so I have a do while like this:

char buf[1000]

do{

    fgets(buf, 1000, stdin);
    
    //More code here

while(strcmp(buf, "bye")!=0);

But I have a problem that it is, when user made a space or a tab or multiple spaces before write bye the strcmp doesnt recognize him as bye, so the program only exits when users only type bye without any previous space or tab.

I want to know how can I prevent the spaces or tabs before the by word to exits if user type for example:

'     bye'
3 Answers

fgets reads in a complete line, including any starting white spaces and also including the new line character at the end of the line. Even if a user does not enter preceding white spaces, the content of buf is likely to be "bye\n", such that buf will hardly compare equal to "bye" when using strcmp.

I'd suggest to scan the buffer for a single word using sscanf and compare this word to "bye" then. Note that scanf("%s"...) skips white space characters at the beginning of a word and stops before the first white space after the word.

   char isStopWord[20];
   isStopWord[0] = '\0';
   sscanf(buf,"%19s",isStopWord);
}
while(strcmp(isStopWord, "bye")!=0);

It a very strange requirement to use strcmp to remove characters from the array but everything is possible :)

char *removeSpacesAndTabsBeginning(char *str)
{
    size_t len;
    char savedc;
    if(str && *str)
    {
        len = strlen(str);
        savedc = str[1];
        str[1] = 0;
        while(!strcmp(str, " "))
        {
            str[1] = savedc;
            memmove(str, str + 1, len);
            savedc = str[1];
            str[1] = 0;
        }
        str[1] = savedc;
    }
    return str;
}

int main(void)
{
    char z[] = "  \t\tHello World.";
    printf("`%s`\n", removeSpacesAndTabsBeginning(z));
}

If you want to know if the string contains "bye" use strstr.

int isBye(const char *str)
{
    return !!strstr(str, "bye");
}

There are 2 problems in your code:

  • you should test the return value of fgets() to detect unexpected end of file.
  • you should ignore whitespace in the comparison. You should just trim the string read by fgets() as the other code probably does not handle these either.
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main() {
    char buf[1000];
    while (fgets(buf, sizeof buf, stdin)) {
        size_t i = 0, length = strlen(buf);
        /* trim trailing white space */
        while (length > 0 && isspace((unsigned char)buf[length - 1])) {
            buf[--length] = '\0';
        }
        /* trim leading white space */
        while (isspace((unsigned char)buf[i]) {
            i++;
        }
        if (i > 0) {
            memmove(buf, buf + i, length - i + 1);
        }
        // check for end command
        if (!strcmp(buf, "bye"))
            break;
    
        //More code here: handle other commands
        //...
    }
    return 0;
}
Related