Java String: Sample Problems Solved
Some sample string problems solved
Sample String Problems Solved
/**
* Various string problems solved
*
* @author (Mister V)
* @version (1.0)
*/
public class SampleStringCode
{
public SampleStringCode()
{
System.out.println("Welcome");
System.out.println("these are some examples of how solve some problems");
}
/**
* strBackwards
* given any string, strBackwards returns the string backwards
*
* @param str String
* @return the String backwards
*/
public String strBackwards(String str)
{ String strBackwards="";
for(int i=0;i<str.length();i++){
String letter = str.substring(i,i+1);
strBackwards=letter + strBackwards;
}
return strBackwards;
}
/**
* noVowels
* given any string, noVowels will replace all vowels with astericks
*
* @param str String
* @return the String backwards
*/
public String noVowels(String str){
String strNoVowels="";
for(int i=0;i<str.length();i++){
String letter = str.substring(i,i+1);
if (isaVowel(letter)){letter="*";}
strNoVowels=strNoVowels+letter;
}
return strNoVowels;
}
/**
* isaVowel
*
* @param ltr a single character in a String
* @return true if the character is a vowel, otherwise false
*/
private boolean isaVowel(String ltr){
boolean isVowel=false;
String vowels ="aeiouAEIOU";
if (vowels.indexOf(ltr)>=0){
isVowel = true;
}
return isVowel;
}
/**
* noVowels2a
* given any string, noVowels will replace all vowels with astericks
*
* @param str any valid sting
* @return the String with all vowels replaced with astericks
*/
public String noVowels2a(String str){
str=str.replace('a','*');
str=str.replace('e','*');
str=str.replace('i','*');
str=str.replace('o','*');
str=str.replace('u','*');
str=str.replace('A','*');
str=str.replace('E','*');
str=str.replace('I','*');
str=str.replace('O','*');
str=str.replace('U','*');
return str;
}
/**
* noVowels2b
* given any string, noVowels will replace all vowels with astericks
*
* @param str any valid sting
* @return the String with all vowels replaced with astericks
*
* Notice that I used method noVowels2a as a base for this methods
* I add in aspects of isaVowel
*/
public String noVowels2b(String str){
String vowels ="aeiouAEIOU";
for (int i=0;i<vowels.length();i++){
str=str.replace(vowels.charAt(i),'*');
}
return str;
}
/**
* printUpAndDown displays the phrase verticlly and both forwards and backwards
*
*/
public void printUpAndDown(String phrase){
for(int i=0;i<phrase.length();i++){
System.out.print(phrase.charAt(i));
System.out.print(" ");
System.out.println(phrase.charAt(phrase.length()-i-1));
}
}
}