Search code examples
cexitstrcmp

How to exit the program immediately using strcmp() without printing the next line


The code itself is about typing a string and a character, and the program printing out what position the character is in the particular string.

What I want to add to this is to exit the program immediately when the word "end" is inputed, using strcmp().

The current problem is that although I type in the word "end", I need to type in a character in order for the program to end.

What I want is for the program to end immediately after typing in "end".

#include <stdio.h>
#include <string.h>

void input(char*, char*);
int strcheck(char*, char);

int main()
{
    int count;
    char str[100], ch;

    while (1) {
        input(str, &ch);
        if (strcmp(str, "end") == 0) {      //I'm guessing this part is the problem
            break;
        }
        else {
            count = strcheck(str, ch);
            if (count == -1) {
                printf("\"%s\" string doesn't have '%c'. \n\n", str, ch);
            }
            else {
                printf("\"%s\" string has '%c' in position number %d .\n\n", str, ch, count + 1);

            }
        }
    }
    return 0;
}

void input(char* strp, char* chp)
{
    printf("# enter string : ");
    scanf("%s", strp);
    printf("# enter character : ");
    scanf(" %c", chp);
    return;
}

int strcheck(char* strp, char chp)
{
    int i;
    int size = strlen(strp);
    for (i = 0; i < size; i++) {
        if (strp[i] == chp) {
            return i;
        }
    }
    return -1;
}

The result for this code comes out like...

# enter string : end
# enter character : a

But what I want is for it to...

# enter string : end    //--> exit immediately

Solution

  • The problem is that the character is always asked before the string is checked, that happens because the string and character input routine are in the input function which is completely executed before the string is checked, there are multiple ways to solve this.

    The requirement is that the string must be checked before the character, one way to do it is to move this check to the input function:

    void input(char *strp, char *chp)
    {
        printf("# enter string : ");
        scanf(" %99s", strp); // the input size should be limited to the container capacity
                              // %99s -> 99 characters + null terminator at most
        if (strcmp(strp, "end") == 0)
        {
            exit(0);  //exit() requires #include <stdlib.h>
        }
    
        printf("# enter character : ");
        scanf(" %c", chp);
        return;
    }
    
    int main()
    {
        int count;
        char str[100], ch;
    
        while (1)
        {
            input(str, &ch);
    
            count = strcheck(str, ch);
            if (count == -1)
            {
                printf("\"%s\" string doesn't have '%c'. \n\n", str, ch);
            }
            else
            {
                printf("\"%s\" string has '%c' in position number %d .\n\n", str, ch, count + 1);
            }
        }
        return 0;
    }