+ 1

Stack

How can I reverse a name with stack??

22nd Feb 2019, 4:20 PM
Sama
2 Respuestas
+ 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
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
thanks🙏😇
22nd Feb 2019, 6:13 PM
Sama