AP® Computer Science A glossary
String traversal
Also called: traversing a string
Course: AP® Computer Science A Shows up in: Unit 2: Selection and Iteration
Visiting the characters or substrings of a string one position at a time with a loop, usually with substring(i, i + 1).
Appears in the course and exam description (CED): 2.10.A.1
Indexes run from 0 to length() - 1. Compare substrings with equals, never ==. Standard string algorithms check a property, count matches or build a reversed string.
String word = "code";
String reversed = "";
for (int i = 0; i < word.length(); i++) {
reversed = word.substring(i, i + 1) + reversed;
}
System.out.println(reversed);
Output:
edoc Learn it properly, with a checkpoint 2.10 Implementing String Algorithms