Sam C
Sam C

Reputation: 3

Case Sensitivity in C (scanf) (if/else)

This part of my script works fine except for the input "blue" completes the if statement but the input "Blue" is considered else. I am having trouble making an or statement with || so that "blue" or "Blue" is acceptable. If anyone could provide some guidance it would be greatly appreciated. I just started learning C.

printf("What is your favorite color?\n");
scanf("%s", &color);
if (strcmp(color, "blue") == 0) {
  printf("Your favorite color is %s. Me too!\n\n", color);
} else {
  printf("Your favorite color is %s. That is cool. My favorite color is blue.\n\n", color);
}

Full Script Here:

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

int main(void)

{
    char first [20];
    char last [20];
    char color [20];
    int n;

    printf("Please input your first name: ");
    scanf("%s", &first);
    printf("Please input your last name: ");
    scanf("%s", &last);
    printf("Your name is %s %s.\n\n", first,last);
    printf("What is your favorite color?\n");
    scanf("%s", &color);
        if (strcmp(color, "blue") == 0) {
    printf("Your favorite color is %s. Me too!\n\n", color);
        }
        else {
    printf("Your favorite color is %s. That is cool. My favorite color is blue.\n\n", color);
        }
     printf("I am thinking of a number between 1 and 100. Can you guess it?\n");
    scanf("%d", &n);
        if (n == 54) {
    printf("The number is 54! That is correct!\n\n", n);
        }
        else {
    printf("Wrong the number is not %d. The number was 54.\n\n", n);
        }
    printf("Your name is %s %s. Your favorite color is %s. You guessed the number %d.\n\n", first,last,color,n);
    return(0);
}

Upvotes: 0

Views: 4204

Answers (4)

yash101
yash101

Reputation: 671

B and b have different char values, so they are not equal. There is a very robust way to solve this problem -- convert the entire string to either uppercase or lowercase before you do the comparison. This will ensure that Blue, BLUE, blue and BlUe are the same values. I have a few functions for case-changing in my library, devlib. Here are the actual functions:

#include <algorithm>
#include <string>
#include <cstring>

std::string toLower(std::string line)
{
    std::string tmp = line;
    std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
    return tmp;
}

std::string toUpper(std::string line)
{
    std::string tmp = line;
    std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::toupper);
    return tmp;
}

Alternatively, you can create a case-insensititive function,

bool compareToIgnoreCase(std::string aString, std::string anotherString)
{
    return !strcmp(toLower(aString).c_str(), toLower(anotherString).c_str());
}

Upvotes: 0

Ryan Haining
Ryan Haining

Reputation: 36782

If you don't want to rely on platform specific headers, you can convert the whole input string to lowercase pretty easily:

void lower_string(char *str) {
  for (char *p = str; *p != '\0'; ++p) {
    *p = tolower(*p);
  }
}

and then

lower_string(color);

or roll your own case insensitive comparison

int strcmp_insensitive(const char *str1, const char *str2) {
  for (; *str1 && *str2; ++str1, ++str2) {
    if (tolower(*str1) > tolower(*str2)) return 1;
    if (tolower(*str1) < tolower(*str2)) return -1;
  }
  if (*str1 == *str2) return 0;
  if (*str1 == '\0') return -1;
  return 1;
}

though for your simple purposes I'd recommend using glampert's answer.

Upvotes: 1

glampert
glampert

Reputation: 4411

strcmp(), as you have noticed, is case-sensitive. You can make your code to work with other input formats by using a logical OR ||. E.g.:

if ((strcmp(color, "blue") == 0) || (strcmp(color, "Blue") == 0))

But this is quite tedious to write and would not work for input "BLUE".

If on a Unix based system, you can use strcasecmp() for a case-insensitive compare. Unfortunately this function is not standard. On Windows, an equivalent is stricmp().

Upvotes: 2

merlin2011
merlin2011

Reputation: 75545

Just use || directly to accept both?

if (strcmp(color, "blue") == 0 || strcmp(color, "Blue") == 0)

Upvotes: 2

Related Questions