0
Reverse a sentence by stack
4 Respostas
+ 2
You made a stack of string so its wrong. here is the corrected code -
https://code.sololearn.com/cZMJmdN3pKqZ/?ref=app
+ 2
In the condition of your inner while loop, you need to check first if 'i' is smaller than the length, before accessing the character at position 'i'. Right now, you compare the character at position 'i' to a space character, before knowing whether 'i' is a valid position in the string. The correct condition would be:
...
while ( i < len && s.at( i ) != ' ' )
...
This way around works because the && operator is mandatory to have a sequence point after the first clause, and won't execute the second expression in case the first already evaluates to false.
0
get each char of the sentence onto stack and after pop each letter from there.
0
Input My name is Harsh
Output Harsh is name myThis kind of output I want