+ 1
Is it possible to use a string as condition and the string is our input ?? See my example below đđ
For example If my input is hello, it prints hi If my input is farewell, it prints bye.. Please help me to finish my program
10 Answers
+ 1
string a;
cin >> a;
If(a == "hello"){
cout << "hi";
}
else if (a == "farewell"){
cout << "bye";
}
else
cout << "not match";
+ 1
Thanks bro
0
Oh thank you..
0
In the same thing how I implement the switch case??
0
hanush we cannot pass string in switch we can only check cases for int and char data type. So this case is not possible to implement with switch
0
You need compare() method for this.
A good practice to compare two string is
1. Compare the 1st character of both the strings 1st.
2. If the above condition satisfied, compare their lengths.
3. If both of them are true then use compare() method.
By this approach time complexity got reduced.
0
Martin Taylor You can compare two strings using ==.The C-style string is converted implicitly to a string object and then the operator==() function is invoked which ultimately calls the compare() function