+ 2

String in c++

Can anyone help me to correct this code and also explain it how . The user enter a string and a character, if that character exits in the string it should show the index, otherwise it should show -1. https://code.sololearn.com/cXa4Uspk8acy/?ref=app

17th Dec 2019, 4:41 PM
Armina
Armina - avatar
13 Answers
+ 2
sorry I dont know c++
17th Dec 2019, 4:47 PM
ÃbhĂŻĆĄhĂȘÄ· pãƂ
ÃbhĂŻĆĄhĂȘÄ· pãƂ - avatar
+ 2
You're code doesn't work, and it's completely normal. n can't never be equal to i since the loop breaks when i equals n. But that's not the main issue. To input a string, that was true : cin.getline(s, 20); Then, you want to input a char, so, do like that: char c; cin >> c; Then, the loop was wrong. int i = 0; for(i ; i < 20 ; i++) { if(s[i] == c) flag = true ... // then it was right } And I advise you rewriting it in a 'c++ way'.
17th Dec 2019, 5:29 PM
Théophile
Théophile - avatar
+ 2
After the line : flag = true; Add the break keyword : if(s[i] == c) { flag = true; break; } And it doesn't show an error. It shows a warning. Simply ignore it, it doesn't matter...
17th Dec 2019, 5:58 PM
Théophile
Théophile - avatar
+ 2
It worked for me. When I entered Hello and then the char 'o', it outputs 4. Maybe the problem is when you input the string then the char in SoloLearn. Do you write on a first line your string, and then on a second line the char? I don't really see what you mean by 'assign 0 to i just in the loop'. i is a variable that is a type of counter. So it must hold value 0, so yes, you must assign 0 to i.
17th Dec 2019, 6:06 PM
Théophile
Théophile - avatar
+ 1
Théophile Thanks for your help, I edited the code but I don't know why when I assign 0 to i at the beginning (not in the loop) it shows an error. Also , when I assign 0 to i in the loop , it always shows 20 which is the string lenght.
17th Dec 2019, 5:46 PM
Armina
Armina - avatar
+ 1
I did it but the output is not correct , also why I should assign 0 to i just in the loop?
17th Dec 2019, 6:00 PM
Armina
Armina - avatar
+ 1
Thanks Théophile I write the character on the first line and that was my fault. I got it ,Thanks a lot .
17th Dec 2019, 6:14 PM
Armina
Armina - avatar
+ 1
You're welcome! 👍
17th Dec 2019, 6:16 PM
Théophile
Théophile - avatar
+ 1
#include <iostream> #include <string> using namespace std; int main() { int i=0; char c; bool flag=false ; char s[20]; cout<<"enter string: \n"; cin.getline(s,20); cin>>c; for (; i<20 ; i++) { if (s[i]==c) { flag = true; break; } } if (flag==true) cout<<i; else cout<<"-1"; return 0; } Try this Its Runs
18th Dec 2019, 1:30 PM
Rohit wadne
+ 1
Curly brackets not necessary in this Case. If You Want more than one Operations for the FOR LOOP then You have to put curly brackets.
18th Dec 2019, 2:02 PM
Rohit wadne
+ 1
Rohit wadne i know for more than one operation , we need curly brackets ; but shouldn't the IF statement iterate which each iteration of the FOR LOOP? can you explain why the IF statement shouldn't iterate?
18th Dec 2019, 2:26 PM
Armina
Armina - avatar
+ 1
#include <iostream> #include <string> using namespace std; int main() { string c; string s; cout<<"enter string: \n"; getline(cin,s); cout << "Enter char to find:"; getline(cin,c); int flag =s.find(c); if (s.find(c) != string::npos) cout<<flag; else cout<<"-1"; return 0; }
19th Dec 2019, 2:01 PM
Daramola Oluwafemi Michael
Daramola Oluwafemi Michael - avatar
0
If statement Are used To check Whether some operation True Or Not. They are not Like For Loop
22nd Dec 2019, 12:41 PM
Rohit wadne