C++

How to Use Switch Statement for Strings in C++

In C++, a switch statement is a control flow statement that enables you to choose one of several execution pathways based on the value of a variable. Programmers may create complicated decision-making systems using the switch statement.

A series of case labels are compared to the value of an input variable or expression using the switch statement. The input variable is tested against the case labels, which are values, and if a match is found, the case statement’s code is run.

The switch statement in C++ is frequently used when working with characters and numbers, but it may also be used with strings to evaluate many conditions more quickly. In this article, we’ll demonstrate how to use strings with the C++ switch statement.

Switch Statement with Strings

The switch case only evaluates integer expressions, hence there is a catch when utilizing the string class. String comparisons cannot be made directly in a switch statement. In order to properly evaluate the string, we must first transform it into a numerical number.

So, we can utilize the string class and its member methods to use the switch statement with strings. The string class has a number of methods that may be used to manage strings, including compare(), find(), and substr() functions.

We can first declare a string variable and give it a starting value. The switch statement may then be used with the string variable. Think about the following code, for example:

#include <iostream>
using namespace std;

int main()
{
    string fruit = "apple";

    switch(fruit) {
    case "banana":
    cout << "This is a banana." << endl;
    break;
    case "orange":
    cout << "This is an orange." << endl;
    break;
    case "apple":
    cout << "This is an apple." << endl;
    break;
    default:
    cout << "This is not a valid fruit." << endl;
    break;
    }
    return 0;
}

In this illustration, the initial value of the string variable “fruit” is “apple”. It’s a fruit is printed after the switch statement analyzes the variable “fruit”. It prints “That’s a vegetable” if “carrot” is the value of “fruit.” If none of the criteria are met, “Invalid input” is printed.

Output

The above code will give an error that the switch quantity is not an integer. To fix this following solution can be used.

Switch Statement on String Using compare() Function

Fortunately, the “compare()” member function of the string class produces an integer value that represents the comparison outcome of two strings. The comparison function gives a result of 0 if the strings are equal, a number that is either negative or positive depending on whether the first string is more or smaller than the second string.

Therefore, we can use the compare() function to compare strings in switch cases. Here’s an example of using the compare function:

#include <iostream>
using namespace std;

int main()
{
    string fruit = "apple";
    switch (fruit.compare("apple")) {
    case 0:
        cout << "It's a fruit." << endl;
        break;
    case -1:
        cout << "It's a vegetable." << endl;
        break;
    default:
        cout << "Invalid input." << endl;
}
    return 0;
}

In the above example, we used the compare function to compare the strings “fruit” and “apple.” If the result of the comparison is zero, the message “It’s a fruit” is printed. It prints “That’s a vegetable” if the comparison result is negative. If not, it displays “Invalid input.”

Output

It’s important to note that strings must be enclosed in double quotations (“”) when used as case labels. Single quotations (“) will result in a compilation error.

Programming’s switch statement is an effective tool because it enables complicated decisions to be made based on a single input variable. When you need to structure your code around a lot of different possible outcomes, it is very helpful.

The switch statement’s ability to utilize strings as case labels is another fantastic feature. Later versions of C++, including C++11, include this capability.

Conclusion

The switch statement in C++ is a control flow statement that enables complicated decision-making based on a single input variable. It’s an excellent tool for structuring your code and managing a lot of potential outcomes. The switch statement gains extra versatility by supporting the use of strings as case labels, making it a more useful tool for C++ writers.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.