Home  >  Article  >  Backend Development  >  C++ program to convert string type variable to boolean type

C++ program to convert string type variable to boolean type

WBOY
WBOYforward
2023-09-15 17:49:021179browse

C++ program to convert string type variable to boolean type

In C, Boolean variables consist of binary data true or false, and string variables are sequences of letters, numbers, and special characters. The compiler itself cannot convert a string to a boolean, but there are several ways to perform this conversion. We explore various methods of converting string values ​​to Boolean values.

If we think about the algorithm, it's pretty simple. We take a string value and convert it to a boolean value using various means.

Algorithm (generalized)

  • Get input in a string variable.
  • Convert a string value (true or false) to a Boolean value.
  • output value.

Use boolalpha and isringstream

Boolalpha is a stream I/O manipulator that can be used to manipulate Boolean and alphanumeric values. Istringstream is a string stream used to implement different functions on character streams. Since boolalpha works with streams, it can be used with isringstream to convert string values ​​to boolean values.

grammar

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

algorithm

  • Get input in a string variable.
  • Put the value into the isstringstream object and use boolalpha to assign the value to the boolean variable.
  • output 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 take a string value as input. We then use an isringstream object to contain the string value and then use the boolalpha modifier to convert it to a boolean variable. We print the input and output values ​​for comparison.

Use 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

  • Get input in the string variable ip.
  • Use Boolean variable operations.
  • If ip is the same as "false", then
    • op = false
  • otherwise,
    • 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

Use std::stoi

In the previous example, we only converted "true" to the Boolean value "1" and "false" to the Boolean value "0". Now, in some cases the string value may be 0 or 1. For this case, we can use the stoi function to convert the string value to an integer and then to a boolean value. The stoi function converts a string value to an integer and using explicit type conversion we can convert the value to a boolean value.

grammar

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

algorithm

  • Get input in the string variable ip.
  • Use Boolean variable operations.
  • Explicitly convert the value to bool as 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

in conclusion

We take a string as input, which may contain any value "true", "1", "false" or "0". The first two methods convert "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 '1' or '0' to 'true' or 'false', it will not work because stoi function cannot convert string that does not contain alphanumeric characters to integer value and therefore cannot be converted to a Boolean value. So, depending on the use case, we have to determine the best method to use.

When using certain third-party libraries or APIs in specific projects, string to Boolean conversion is required. Some APIs or libraries output in string format, in order to make the result compatible, we have to convert the string value to boolean value.

The above is the detailed content of C++ program to convert string type variable to boolean type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete