// Allison Obourn
// CS 141, Winter 2021
// Lecture 8

// This program prints three phrases backward and then prints four phrases
// with each pair of letters swapped in order. 
// It then ciphers some text with a Ceasar Cipher and prints out the result

public class StringExamples {
   public static void main(String[] args) {
   /*   printBackward("hello there!");
      printBackward("cat");
      printBackward("");
      
      System.out.println(switchPairs("example"));
      System.out.println(switchPairs("john"));
      System.out.println(switchPairs(""));
      System.out.println(switchPairs("c"));
      
      cipher("hello world", 7);*/
      
      printLetters("Atmosphere");
   }
   
   // prints out the passed in string backwards and then moves to a new line
   public static void printBackward(String word) {
      for(int i = word.length() - 1; i >= 0; i--) {
         System.out.print(word.charAt(i));
      }
    
      // This loop will do the same thing
      // for(int i = 0; i < word.length(); i++) {
      //     System.out.print(word.charAt(word.length() - i - 1));
      // }

      System.out.println();
   }
   
   // returns a new string consisting of the characters of the passed in string 
   // with each pair of characters swapped in order. If the string has an odd number 
   // of characters the last character still occurs at the end.
   public static String switchPairs(String word) {
      String result = "";
      for(int i = 0; i < word.length() - 1; i += 2) {
         char first = word.charAt(i);
         char second = word.charAt(i + 1);
         result = result + second + first;
      }
      // add the last character if the string has an odd length
      if(word.length() % 2 == 1) {
         result += word.charAt(word.length() - 1);
      }
      return result;
   }
   
   // prints out the passed in string with each alphabetic character
   // shifted by the key amount. Shifts all characters to lowercase.
   // Preserves all non alphabetic characters
   public static void cipher(String text, int key) {
      text = text.toLowerCase();
      for(int i = 0; i < text.length(); i++) {
         char current = text.charAt(i);
         if(current >= 'a' && current <= 'z') {
            current = (char)(current + key);
            if(current > 'z') {
               current = (char)(current - 26);
            } else if (current < 'a') {
               current = (char)(current + 26);
            }
         }
         System.out.print(current);
      }
   }  
   
   // prints the letters of the string separated with commas
   // and spaces
   public static void printLetters(String word) {
      // the commented out code does the same thing as the 
      // code below
      /* System.out.print(word.charAt(0));
         for(int i = 1; i < word.length(); i++) {
            char current = word.charAt(i);
            System.out.print(", " + current);
         } */
      
      for(int i = 0; i < word.length() - 1; i++) {
         char current = word.charAt(i);
         System.out.print(current + ", ");
      }
      int last = word.length() - 1;
      System.out.print(word.charAt(last));
   }
   
}