Pseudocode Example
Week 08
Example 1: String reverse
Design an algorithm that reverses a string and print it.
• Sample Answer using For loop
Algorithm start
Declare an array of string type variable called word
Declare a loopcounter
Store a string in the array word
For loopcounter = (length of the word) – 1 to 0
loopcounter = loopcounter – 1
print arrayword [loopcounter]
End for
Algorithm end
2
Same example using While
Algorithm start
Declare an array of string type variable called word
Declare a loopcounter
Store a string in the array word
Put loopcounter= length of the word
DoWhile(loopcounter greater than Zero)
loopcounter = loopcounter – 1
print arrayword [loopcounter]
Loop
Algorithm end
3
1
Week 08
Example 1: String reverse
Design an algorithm that reverses a string and print it.
• Sample Answer using For loop
Algorithm start
Declare an array of string type variable called word
Declare a loopcounter
Store a string in the array word
For loopcounter = (length of the word) – 1 to 0
loopcounter = loopcounter – 1
print arrayword [loopcounter]
End for
Algorithm end
2
Same example using While
Algorithm start
Declare an array of string type variable called word
Declare a loopcounter
Store a string in the array word
Put loopcounter= length of the word
DoWhile(loopcounter greater than Zero)
loopcounter = loopcounter – 1
print arrayword [loopcounter]
Loop
Algorithm end
3
1