Skip to the content.
4.1 While Loop 4.2 For Loop 4.3 String Iteration 4.4 Nested Iteration Unit 4 HW Quiz

Unit 4.3 - String Iteration

Unit 4 Team Teach

4.3 String Iteration

simple for loop to iterate through every character using index with charAt():

String word = "hello";
for (int i = 0; i < word.length(); i++) {
    System.out.println(word.charAt(i));
}

in order to use an enhanced for loop, a character array is needed.
toCharArray() can help accomplish this.
example of iteration with an enhanced for loop:

String word = "hello";
for (char c : word.toCharArray()) {
    System.out.println(c);
}
h
e
l
l
o

Popcorn Hack:

Iterate through the characters a string with a while loop

String word = "hello";
int index = 0;
while (index < 5) { 
    System.out.println(word.charAt(index));
    index += 1;
}
h
e
l
l
o

What is a substring?

  • a substring is a subset of the main string
  • the substring(a,b) method creates a substring with the characters of the original string with indices of a to b.
  • string.length() returns the length of the string
  • string1.equals(string2) determines if the two strings have the same characters
String word = "sunflower";
String sub = "low";
boolean found = false;
for (int i = 0; i < word.length(); i++) {
    String portion = word.substring(i, i+sub.length());
    if (portion.equals(sub)){
        found = true;
    }
}

System.out.println("is " + )

Iterating through words

use split() to split a string into an array.
then we can iterate through the array to access each individual word

String phrase = "this is a string";
String[] words = phrase.split(" ");
for (String word : words) {
    System.out.println(word);
}

Homework Hack!

code a caesar cipher that will encrypt any string with any key provided.
your code should go into the encrypt() method, and should successfully pass the test cases provided
as a bonus, try to use StringBuilder

public class CaesarCipher {
    private int key;
    private String phrase;

    public CaesarCipher(int key, String phrase) {
        this.key = key;
        this.phrase = phrase;
    }

    public String encrypt() {
        StringBuilder encrypted = new StringBuilder();

        for (char c : phrase.toCharArray()) {
            if (Character.isLetter(c)) {
                char base = Character.isUpperCase(c) ? 'A' : 'a';
                char shifted = (char) ((c - base + key) % 26 + base);
                encrypted.append(shifted);
            } else {
                encrypted.append(c);
            }
        }

        return encrypted.toString();
    }

    public static void main(String[] args) {
        CaesarCipher test1 = new CaesarCipher(3, "hello world");
        CaesarCipher test2 = new CaesarCipher(10, "abcdefg");
        CaesarCipher test3 = new CaesarCipher(20, "i love csa");

        System.out.println("test 1: " + test1.encrypt());
        System.out.println("test 2: " + test2.encrypt());
        System.out.println("test 3: " + test3.encrypt());
    }
}
CaesarCipher test1 = new CaesarCipher(3, "hello world");
CaesarCipher test2 = new CaesarCipher(10, "abcdefg");
CaesarCipher test3 = new CaesarCipher(20, "i love csa");

System.out.println("test 1: " + test1.encrypt());
System.out.println("test 2: " + test2.encrypt());
System.out.println("test 3: " + test3.encrypt());
test 1: khoor zruog
test 2: klmnopq
test 3: c fipy wmu
2
15


Iteration: 0

Current Velocity: 2, 2