Stack | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Stack

How can I reverse a name with stack??

22nd Feb 2019, 4:20 PM
Sama
2 Answers
+ 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