C++ Program to Convert String Type Variables into Boolean


In C++, Boolean variables consists of binary data true or false and string variables are a sequence of alphabets, numerals, and special characters. Conversion between string to Boolean isn’t possible natively by the compiler, but there are several ways to perform this conversion. We explore the various ways using which we can convert a string value into a Boolean.

If we think about the algorithm , it is quite easy. We take a string value and convert into Boolean using various ways.

Algorithm (generalized)

  • Take input in a string variable.
  • Convert the string value (true or false) into a Boolean value.
  • Output the value.

Using boolalpha and istringstream

Boolalpha is an stream I/O manipulator and can be used to manipulate Boolean and alphanumeric values. Istringstream is a string stream which is used to implement different functions on a character stream. As boolalpha works with streams, it can be used with istringstream to convert a string value to a Boolean value.

Syntax

string ip = <string literal>;
bool op;
istringstream(ip) >> std::boolalpha >> op;

Algorithm

  • Take input in a string variable.
  • Put the value in a istringstream object and assign the value to the Boolean variable using boolalpha.
  • Output the value.

Example

#include <iostream> #include<sstream> using namespace std; bool solve(string ip) { bool op; istringstream(ip) >> std::boolalpha >> op; return op; } int main() { string ip = "true"; bool op = solve(ip); cout << "The value of ip is: " << ip <<endl; cout << "The value of op is: " << op <<endl; return 0; }

Output

The value of ip is: true
The value of op is: 1

In this example, we have taken an string value as input. We then use a istringstream object to contain the string value and then using the boolalpha modifier we converted it into a Boolean variable. We print the input and output values for comparison.

Using string comparison

In the next example, we have done a basic string comparison to convert a string value into a Boolean value. If the string value is equal to ‘false’, then 0 is returned; otherwise, 1 is returned. One thing is to be noted that this returns true for all strings other than ‘false’. But this method is the easiest to implement.

Syntax

string ip = <string literal>;
bool op = ip != “false”;

Algorithm

  • Take input in a string variable ip.
  • Take a Boolean variable op.
  • If ip is same as “false”, then
    • op = false
  • Else,
    • op = true
  • Display the value of op.

Example

#include <iostream> using namespace std; bool solve(string s) { return s != "false"; } using namespace std; int main() { string ip = "true"; bool op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }

Output

The input value is: true
The output value is: 1

Using std::stoi

In the previous examples, we have only translated ‘true’ to Boolean 1 and ‘false’ to Boolean ‘0’. Now, in some circumstances, the string value maybe 0 or 1. For that case, we can convert the string value to an integer and then to Boolean using stoi function. The stoi function converts string value to integer and using explicit typecasting, we can convert that value into Boolean.

Syntax

string ip = <string literal>;
bool op = (bool)stoi(ip);

Algorithm

  • Take input in a string variable ip.
  • Take a Boolean variable op.
  • Explicitly typecast the value to bool to the result of stoi(ip).
  • Display the value of op.

Example

#include <iostream> #include <string> using namespace std; bool solve(string s) { //using std:: stoi function return (bool)stoi(s); } using namespace std; int main() { string ip = "1"; bool op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }

Output

The input value is: 1
The output value is: 1

Conclusion

We have taken strings as input that may contain any of the values ‘true’, ‘1’, ‘false’, or ‘0’. The first two methods converts ‘true’ or ‘false’ to 1 and 0 respectively. If we replace ‘true’ or ‘false’ with ‘1’ or ‘0’, it will work the same way. But in the third example, if we change the ‘1’ or ‘0’ to ‘true’ or ‘false’, it will not work because the stoi function cannot convert a string that does not contain alphanumeric characters to integer values, and thus cannot be casted to Boolean. So, depending on the usage we have to determine the best method to be used.

String to Boolean conversions are required when some third party libraries or APIs are used in a particular project. Some APIs or libraries output in a string format and to make the result compatible, we have to convert the string values into Boolean.

Updated on: 19-Oct-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements