+ 1
Cpp string
I want "compare" to take sen[i] values , then compare it . It works in c But in cpp i dont know why it doesn't take values Doesnt string data type act like an array ? https://code.sololearn.com/c9YkieY82aSj/?ref=app
8 Respostas
+ 3
Dark,
You don’t need to count commas or to use find algo, you can loop and compare like you did but it doesn’t need to be so complex.
Here, walk through this example:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string boxes, fword;
cin >> boxes >> fword;
istringstream ss(boxes);
string word;
int i{};
while(getline(ss, word, ',')) {
i += 5;
if (word == fword) {
cout << i;
break;
}
}
return 0;
}
+ 2
Dark,
I think I get what you are trying to do but unsure why…
Strings can act like arrays, however you haven’t intialized the string ‘compare’ - it’s length is zero….so if you reference it like an array its out of bounds.
cin>>sen>>word;
compare.resize(50);
Would demonstrate it working - but your code is in no way good. Using goto like that inside if-else is not needed and makes the code hard to read.
Explain your goal properly and we can suggest better methods 👍
+ 1
DavX here is the problem
You are robbing a bank, but you’re not taking everything. You are looking for a specific item in the safety deposit boxes and you are going to drill into each one in order to find your item. Once you find your item you can make your escape, but how long will it take you to get to that item?
Task
Determine the amount of time it will take you to find the item you are looking for if it takes you 5 minutes to drill into each box.
Input Format
A string that represent the items in each box that will be drilled in order (items are separated by a comma), and secondly, a string of which item you are looking for.
Output Format
An integer of the amount of time it will take for you to find your item.
Sample Input
'gold,diamonds,documents,Declaration of Independence,keys'
'Declaration of Independence'
Sample Output
20
Mirielle here is the task .
+ 1
Mirielle would u please put those functions into a code ?
It's my first to know it .
+ 1
Oh dear, not this again…block me and edit your answer (Massively fail? White space? 😂)
getline in my example is working with a stringstream not stdin.
This passes all test cases
Plus we are not looking for any index to be returned…simply looping and counting!