Stack | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

Stack

How can I reverse a name with stack??

22nd Feb 2019, 4:20 PM
Sama
2 Antworten
+ 4
If you name is stored in a character array, then you need to iterate over the string/char array and add its elements to an std::stack object. Then create a new empty string and empty the stack. Due to the stack's LIFO nature, the stack data will be retrieved in reverse, which can be appended to your string. The loop will look like: string rev; while(!stk.empty()) { rev+=stk.top(); stk.pop(); }
22nd Feb 2019, 5:52 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
thanks🙏😇
22nd Feb 2019, 6:13 PM
Sama