+ 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

10th Feb 2023, 5:33 PM
Shimaa
Shimaa - avatar
8 Answers
+ 3
Your question is not comprehensible... I'll suggest you add more context to the description.. however, let me clarify the doubt "Doesn't string data type act like an array" No, string data type in c++ doesn't act like array even by 1%... They act like the standard container and by default they are always initialised to "" with the size of 0 Also the resize() method is a valid operation on any instance of string wether their size is zero or not ..
10th Feb 2023, 6:32 PM
Mirielle
Mirielle - avatar
+ 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; }
10th Feb 2023, 7:59 PM
DavX
DavX - avatar
+ 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 👍
10th Feb 2023, 6:20 PM
DavX
DavX - avatar
+ 2
Dark from your sample input and output, it appears like the time taken is (No of comma + 1) * 5 Irrespective of the index of word that should be found. Should that be the case, I'll suggest you look into the algorithm header You can use std::count() to count all the commas and just multiply it by 5 However, if the index of the word is to be taken into consideration (which I think some testcase would require).. you can also look into std::find() from the algorithm header, it returns an iterator to the first appearance of "," and using while loop.. you can get the index and multiply it by 5
10th Feb 2023, 6:56 PM
Mirielle
Mirielle - avatar
+ 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 .
10th Feb 2023, 6:28 PM
Shimaa
Shimaa - avatar
+ 1
Mirielle would u please put those functions into a code ? It's my first to know it .
10th Feb 2023, 7:44 PM
Shimaa
Shimaa - avatar
+ 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!
10th Feb 2023, 9:28 PM
DavX
DavX - avatar
0
Using getline will likely fail due to how stdin are handle in c++ especially with getline ()... Also get line will not return the index of , as you expect, it will return something else I will recommend you use the efficient algorithm method https://code.sololearn.com/c7kNcRhzQA1x/?ref=app
10th Feb 2023, 9:13 PM
Mirielle
Mirielle - avatar