+ 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

12th Jun 2020, 2:42 PM
hanush
hanush - avatar
10 Respostas
+ 1
string a; cin >> a; If(a == "hello"){ cout << "hi"; } else if (a == "farewell"){ cout << "bye"; } else cout << "not match";
12th Jun 2020, 3:13 PM
Saad Saleem
Saad Saleem - avatar
+ 1
Thanks bro
12th Jun 2020, 4:00 PM
hanush
hanush - avatar
0
Oh thank you..
13th Jun 2020, 4:22 AM
hanush
hanush - avatar
0
In the same thing how I implement the switch case??
13th Jun 2020, 4:29 AM
hanush
hanush - avatar
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
13th Jun 2020, 2:34 PM
Saad Saleem
Saad Saleem - avatar
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.
13th Jun 2020, 7:16 PM
Bibhash Ghosh
Bibhash Ghosh - avatar
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
13th Jun 2020, 10:12 PM
Anthony Maina
Anthony Maina - avatar