//This program shows using a for loop with the charAt String method // for getting each character of a String. //It then has 3 different solutions to to the fence-post problem public class printAwesome{ public static void main(String[] args){ String str = "Awesome"; //print each letter of the string on a new line for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); System.out.println(c); } System.out.println(); System.out.println(); //print comma separated string: // A, w, e, s, o, m, e //solution 1: Print first separately // | --| --| --| --| System.out.print(str.charAt(0)); for(int i = 1; i < str.length(); i++){ System.out.print(", " + str.charAt(i)); } System.out.println(); System.out.println(); //solution 2: Print last separately // |-- |-- |-- |-- | for(int i = 0; i < str.length() - 1; i++){ System.out.print(str.charAt(i) + ", "); } System.out.print(str.charAt(str.length() - 1)); System.out.println(); System.out.println(); //solution 3: use if statement // | -- | -- | -- | -- | for(int i = 0; i < str.length(); i++){ System.out.print(str.charAt(i) ); if(i < str.length() - 1){ System.out.print(", "); } } System.out.println(); } }