– String Solutions

4.1 Count Words

Write a method that returns the number of words in a String

4.1.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(countWords(sentence));
 7:      }
 8:
 9:      int countWords(String s)
10:      {
11:         String [] words = s.split("\\s+");
12:         return words.length;
13:      }
14:
15:      public static void main(String[] args) {
16:          // TODO code application logic here
17:          new NNStrings();
18:      }
19:  }

4.2 Count the letter ‘e’

Write a method that counts the instances of the letter ‘e’ in a string.

4.2.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(countEs(sentence));
 7:      }
 8:
 9:      int countEs(String s)
10:      {
11:          int count = 0;
12:          for(int i=0; i<s.length(); i++)
13:          {
14:              if(s.substring(i,i+1).matches("e"))
15:                  count++;
16:          }
17:          return count;
18:      }
19:
20:      public static void main(String[] args) {
21:          // TODO code application logic here
22:          new NNStrings();
23:      }
24:  }

4.3 Count Alphanumerics

Write a method that returns the number of alphanumeric characters (A-Z, a-z and 0-9) in a string.

4.3.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          String title = "1984 by George Orwell.";
 7:          System.out.println(countChars(sentence));
 8:          System.out.println(countChars(title));
 9:      }
10:
11:      int countChars(String s)
12:      {
13:          int count = 0;
14:          for(int i=0; i<s.length(); i++)
15:          {
16:              if(s.substring(i,i+1).matches("[A-Za-z0-9]"))
17:                  count++;
18:          }
19:          return count;
20:      }
21:
22:      public static void main(String[] args) {
23:          // TODO code application logic here
24:          new NNStrings();
25:      }
26:  }

4.4 Reverse String

Write a method that returns a string, reversed

4.4.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(reverse(sentence));
 7:          System.out.println(reverseH(sentence));
 8:          System.out.println(reverseSB(sentence));
 9:      }
10:
11:      // A Haskell-y solution
12:      String reverse(String s)
13:      {
14:          String reverse= "";
15:          for (int i = 0; i<s.length(); i++)
16:          {
17:              reverse = s.substring(i,i+1) + reverse;
18:          }
19:          return reverse;
20:      }
21:
22:      // A recursive Haskell-y solution
23:      String reverseH(String s)
24:      {
25:          if (s.length() == 0) return "";
26:          else if (s.length() == 1) return s;
27:          else return  reverseH(s.substring(1)) + s.substring(0,1);
28:      }
29:
30:      //A StringBuffer solution
31:      String reverseSB(String s)
32:      {
33:          String reverse = new StringBuffer(s).reverse().toString();
34:          return reverse;
35:      }
36:      public static void main(String[] args) {
37:          // TODO code application logic here
38:          new NNStrings();
39:      }
40:  }

4.5 Palindromes

Write a method to test if a string is a Palindrome

4.5.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          String palindrome = "rotavator";
 7:          System.out.println(isPalindrome(sentence));
 8:          System.out.println(isPalindrome(palindrome));
 9:      }
10:
11:      // A Haskell-y solution
12:      String reverse(String s)
13:      {
14:          String reverse= "";
15:          for (int i = 0; i<s.length(); i++)
16:          {
17:              reverse = s.substring(i,i+1) + reverse;
18:          }
19:          return reverse;
20:      }
21:      boolean isPalindrome(String s)
22:      {
23:          return s.equals(reverse(s)) ? true : false;
24:      }
25:
26:      public static void main(String[] args) {
27:          // TODO code application logic here
28:          new NNStrings();
29:      }
30:  }

4.6 More Palindromes

Write an improved palindrome method that will disregard spaces, punctuation and case and thus recognise sentences such as “A man, a plan, a canal, Panama!” as a palindrome.

4.6.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          System.out.println(isImpPalindrome(pal2));
 6:      }
 7:
 8:      // A Haskell-y solution
 9:      String reverse(String s)
10:      {
11:          String reverse= "";
12:          for (int i = 0; i<s.length(); i++)
13:          {
14:              reverse = s.substring(i,i+1) + reverse;
15:          }
16:          return reverse;
17:      }
18:
19:      boolean isImpPalindrome(String s)
20:      {
21:          String lower = s.toLowerCase();
22:          String stripped = lower.replaceAll("[^A-Za-z0-9]", "");
23:          return stripped.equals(reverse(stripped)) ? true : false;
24:      }
25:
26:      public static void main(String[] args) {
27:          // TODO code application logic here
28:          new NNStrings();
29:      }
30:  }

4.7 Remove v*w*ls

Write a method that will replace the vowels in a word with asterisks

4.7.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(star(sentence));
 7:      }
 8:
 9:
10:      String star(String s)
11:      {
12:          return s.replaceAll("[AEIOUaeiou]", "*");
13:      }
14:
15:      public static void main(String[] args) {
16:          // TODO code application logic here
17:          new NNStrings();
18:      }
19:  }

4.8 Spell Out

Write a method to spell out words so that, for example, This becomes T-H-I-S.

4.8.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          String sentence = "I never saw a purple cow";
 6:          System.out.println(spellOut(sentence));
 7:      }
 8:
 9:      String spellOut(String s)
10:      {
11:          s = s.toUpperCase();
12:          String spell = "";
13:          for (char c: s.toCharArray())
14:          {
15:              spell = spell + c + "-";
16:          }
17:
18:          spell = spell.replaceAll("-\\s+-|-$"," ");
19:          return spell;
20:      }
21:
22:
23:      public static void main(String[] args) {
24:          // TODO code application logic here
25:          new NNStrings();
26:      }
27:  }

4.9 Substitution Cipher

In a simple substitution cipher, A =1 , B=2, C=3 etc. Write a method that encodes a sentence using this cipher

4.9.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          System.out.println(encode("Hello World"));
 6:      }
 7:      String encode(String s)
 8:      {
 9:          String encoding = "";
10:          s=s.toLowerCase();
11:          String [] words = s.split("\\s+");
12:          for (String word : words)
13:          {
14:              for(int i = 0; i<word.length() - 1 ; i++)
15:              {
16:                  encoding = encoding + ((int)word.charAt(i)-96) + ",";
17:              }
18:              encoding = encoding + ((int)word.charAt(word.length()-1)-96) + " ";
19:          }
20:
21:          return encoding;
22:      }
23:
24:      public static void main(String[] args) {
25:          // TODO code application logic here
26:          new NNStrings();
27:      }
28:  }

4.10 Decoder

Write a decoder method for your substitution Cipher

4.10.1 Solution

 1:  public class NNStrings {
 2:
 3:      NNStrings()
 4:      {
 5:          System.out.println(decode("9 14,5,22,5,18 19,1,23 1 16,21,18,16,12,5 3,15,23"));
 6:      }
 7:
 8:      String decode(String s)
 9:      {
10:          String decoding = "";
11:          String [] words = s.split("\\s+");
12:          for (String word : words)
13:          {
14:              String [] codes = word.split(",");
15:              for (String code: codes)
16:              {
17:                  int c = Integer.parseInt(code.trim());
18:                  decoding = decoding + (char)(c+96);
19:              }
20:              decoding = decoding + " ";
21:          }
22:          return decoding;
23:      }
24:
25:      public static void main(String[] args) {
26:          // TODO code application logic here
27:          new NNStrings();
28:      }
29:  }

Leave a Comment