+ 5

How to compare string units?

For a string we can use cout<<name.at[4]; to print it on the screen but how can I compare them like string x; cin>>x; x.at[2] == "A";//It shows error pls help

4th Jul 2018, 4:30 PM
Shahil Ahmed
Shahil Ahmed - avatar
4 Respostas
+ 4
wrap A by using single quote rather than double quote string x; cin>>x; x.at(2) == 'A';
4th Jul 2018, 4:37 PM
MO ELomari
MO ELomari - avatar
+ 4
Thanks Thanks Thanks alot
4th Jul 2018, 5:01 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 3
Can you help me with this code pls https://code.sololearn.com/cp9dy789S7ak/?ref=app
4th Jul 2018, 4:45 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
#include <iostream> // Flip The Binary int main() { std::string bin; // i change the type to string rather than int std::cin >> bin; std::cout << "then: "; std::cout << bin << std::endl; for (int x = 0; x < bin.length(); ++x){ // change the <= operator by < if (bin.at(x) == '0'){ bin.at(x) = '1'; }else if (bin.at(x) == '1'){ // forgot parenthes bin.at(x) = '0'; } } std::cout << "now: "; std::cout << bin << std::endl; }
4th Jul 2018, 4:54 PM
MO ELomari
MO ELomari - avatar