Do-while or while loop that will keep taking an input from user but will terminate after I input the string "exit" (case insensitive) in c language

Viewed 68
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[100];
    char out[] = "exit";

    do {
        printf("Enter a string: ");
        scanf("%s", str);

        // some if else statement here 

    } while (toupper(str[3]) != toupper(out[3]));
}

I put the index 3 because if I put the index 0 there, the code will terminate if the entered string starts with letter e. I tried the while loop but it does not work for me. Also I want to print a prompt message that says "detected terminate keyword" after entering the word "exit" and then terminates the loop.

You will also notice the toupper() function. I used it there because I want my loop to be case insensitive, so regarless of lowercase or uppercase or combination of both, the loop should terminate when the word "exit" is entered.

4 Answers

toupper(str[3]) != toupper(out[3]) will compare the upper case 4th letter of str and out, so the loop will iterate till str[3] is 'T'. You want to use strcasecmp(str, out) instead. Remember to #include <strings.h>.

I put the index 3 because if I put the index 0 there, the code will terminate if the entered string starts with letter e

Exactly, and the code:

while (toupper(str[3]) != toupper(out[3])

Suffers from the same problem, any input with a t as its 4th character index 3 will match and the loop will end, you are comparing a specific character of the string, not the string itself. You can use strcasecmp to assess if the input is indeed exit and ignore casing.

Furthermore using %s specifier is not good, you run the risk of overrunning the destination buffer. You should use a width, %99s for a 100 characters buffer to leave space for the nul byte, if possible consider using fgets instead.

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

int main() {
    
    char str[100];

    char out[] = "exit";

    do {
        printf("Enter a string: ");
        scanf(" %99s", str); // space before specifier to clean leading whitespaces

        // some if else statement here 

    } while (strcasecmp(str, out) != 0);

    puts("Detected terminate keyword. Goodbye!");
}

There are multiple problems:

  • it is confusing for a function isPalindrome() to return 0 for true.

  • to avoid undefined behavior on negative char values, a char argument to toupper should be cast as (unsigned char).

  • the test for the exit keyword is incorrect. You exit if the fourth letter is a t or a T. You should use strcasecmp to test for the exit word.

  • scanf("%s", str) has potential undefined behavior if the user enters a word with more than 99 bytes. Use scanf("%99s", str) and test the return value: it must be 1 for a successful conversion.

  • instead of a confusing do / while loop, use a for (;;) loop (also known as for ever loop), and test for 2 exit conditions: scanf() failure to read a word and reading the word exit.

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

int isPalindrome(const char *str) {
    size_t len = strlen(str);

    for (size_t i = 0; i < len; i++) {
        if (toupper((unsigned char)str[i]) != toupper((unsigned char)str[len - i - 1]))
            return 0;
    }
    return 1;
}

int main() {
    char str[100];

    for (;;) {
        printf("Enter a string: ");
        if (scanf("%99s", str) != 1)
            break;
        if (!strcasecmp(str, "exit"))
            break;

        if (isPalindrome(str)) {
            printf("%s is a palindrome!\n\n", str);
        } else {
            printf("%s is not a palindrome!\n\n", str);
        }  
    }
    return 0;
}
char *removeLastChar(char *str, char ch)
{
    size_t len;
    if(str)
    {
        len = strlen(str);
        if(str[len - 1] == ch) str[len -1] = 0;
    }
    return str;
}

char *strlwr(char *str)
{
    char *wrk = str;
    if(str)
    {
        while(*wrk)
        {
            *wrk = tolower((unsigned char)*wrk);
            wrk++;
        }
    }
    return str;
}

int main(void) 
{
    char str[100];
    const char *out = "exit";

    int x = 0;

    do 
    {
        printf("Enter a string: ");
        if(!fgets(str, sizeof(str), stdin)) break;
        removeLastChar(str, '\n');

        printf("You entered: \"%s\"\n:", str);

     } while (strcmp(strlwr(str), out)); 
}
Related