Problem 1 |
public void yourInitials()
{
String firstName,lastName,firstInitial,lastInitial;
System.out.print("Enter your first name: ");
firstName=in.next();
System.out.print("Enter your last name: ");
lastName=in.next();
firstInitial=firstName.substring(0,1).toUpperCase();
lastInitial=lastName.substring(0,1).toUpperCase();
System.out.print("Your initials are: "+firstInitial+lastInitial);
}
|
Problem 2 |
public void yourMonogram()
{
String name,firstInitial,middleInitial,lastInitial;
int firstSpace,lastSpace;
System.out.print("Enter your name: ");
name=in.nextLine();
firstInitial=name.substring(0,1).toLowerCase();
firstSpace=name.indexOf(" ");
lastSpace=name.lastIndexOf(" ");
middleInitial=name.substring(firstSpace+1,firstSpace+2).toLowerCase();
lastInitial=name.substring(lastSpace+1,lastSpace+2).toUpperCase();
System.out.print("Your monogram is: "+firstInitial+lastInitial+middleInitial);
} |
Problem 3 |
public void replacer()
{
String sentence,searchText,replacementText;
System.out.print("Enter a sentence: ");
sentence=in.nextLine();
System.out.print("Enter the search text: ");
searchText=in.nextLine();
System.out.print("Enter the replacement text: ");
replacementText=in.nextLine();
System.out.println("The new sentence is: "+sentence.replaceAll(searchText,replacementText));
} |
Problem 4 |
public void replaceLetters()
{
String sentence,searchLetter,replacementLetter;
System.out.print("Enter a sentence: ");
sentence=in.nextLine();
System.out.print("Enter the letter to be replaced: ");
searchLetter=in.nextLine();
System.out.print("Enter the replacement letter: ");
replacementLetter=in.nextLine();
System.out.println("The new sentence is: "+sentence.replace(searchLetter,replacementLetter));
} |
Problem 5 |
public void acronymMaker()
{
String sentence,searchLetter,replacementLetter;
int numWords;
System.out.print("Enter the number of words to use: ");
numWords=in.nextInt();
String[] words = new String[numWords+1];
for(int i=1; i<=numWords; i++)
{
System.out.print("Enter word "+i+": ");
words[i]=in.next();
}
System.out.print("The acronym is: ");
for(int i=1; i<=numWords; i++)
{
System.out.print(words[i].substring(0,1).toUpperCase());
}
} |
Problem 6 |
public void reverser()
{
String name;
int length;
System.out.print("Enter your name: ");
name=in.nextLine().toLowerCase();
System.out.print("In reverse order: ");
length=name.length();
for(int i=length-1; i>=0; i--)
System.out.print(name.charAt(i));
// or you could use substring()
//System.out.print(name.substring(i,i+1));
}
|
Problem 7 |
public void vowelCounter()
{
String phrase;
int length,vowels=0;
System.out.print("Enter a word or phrase: ");
phrase=in.nextLine();
length=phrase.length();
for(int i=0; i<length; i++)
{
if(phrase.substring(i,i+1).compareToIgnoreCase("a")==0)
vowels++;
else if(phrase.substring(i,i+1).compareToIgnoreCase("e")==0)
vowels++;
else if(phrase.substring(i,i+1).compareToIgnoreCase("i")==0)
vowels++;
else if(phrase.substring(i,i+1).compareToIgnoreCase("o")==0)
vowels++;
else if(phrase.substring(i,i+1).compareToIgnoreCase("u")==0)
vowels++;
}
System.out.println("The number of vowels is "+vowels);
} |
Problem 8 |
public void wordSplitter()
{
String word;
int length;
char letter;
System.out.print("Enter a word: ");
word=in.next();
length=word.length();
for(int i=0; i<length; i++)
{
letter=word.charAt(i);
System.out.print(letter+"="+(int)letter+" ");
}
} |
Problem 9 |
public void secretCode()
{
String message,choice,numberToDecode;
int length,space1Position=0,space2Position=0,codeNum,lastSpace=0,numSpaces=0,lastNumberLength;
char letter,lastLetter;
System.out.print("Enter the secret message: ");
message=in.nextLine().trim();
length=message.length();
for(int i=0; i<length; i++)
if(message.charAt(i)==' ')
numSpaces++;
System.out.print("The decoded message is: ");
space2Position=message.indexOf(" ");
lastSpace=message.lastIndexOf(" ");
for(int i=0; i<numSpaces; i++)
{
numberToDecode=message.substring(space1Position,space2Position);
codeNum=Integer.parseInt(numberToDecode);
System.out.print((char)codeNum);
space1Position=space2Position+1;
space2Position=message.indexOf(" ",space1Position);
}
lastNumberLength=length-lastSpace;
numberToDecode=message.substring(lastSpace+1,lastSpace+lastNumberLength);
codeNum=Integer.parseInt(numberToDecode);
System.out.print((char)codeNum);
} |
Problem 10 |
public void codifier()
{
String message;
int length,letterNum;
char letter,lastLetter;
System.out.print("Enter a message: ");
message=in.nextLine().trim();
length=message.length();
for(int i=0; i<length; i++)
{
letter=message.charAt(i);
letterNum=(int)letter;
if(letterNum==32)
System.out.print(" ");
else if(letterNum==89)
System.out.print("A");
else if(letterNum==90)
System.out.print("B");
else if(letterNum==121)
System.out.print("a");
else if(letterNum==122)
System.out.print("b");
else
System.out.print((char)(letterNum+2));
}
} |
Problem 11 |
public void palindromeChecker()
{
String text,backwards="";
int length;
char letter,lastLetter;
System.out.print("Enter a word or phrase: ");
text=in.nextLine().trim().toLowerCase();
text=text.replace(" ","");//remove spaces
length=text.length();
for(int i=length-1; i>=0; i--)//build the reverse version of the text
backwards+=text.charAt(i);
if(backwards.equals(text))
System.out.println("You have entered a palindrome.");
else
System.out.println("You have not entered a palindrome.");
}
|
Problem 12 |
public void groupFinder()
{
String name,firstName;
char letter;
int spaceLocation;
System.out.print("Enter a student name: ");
name=in.nextLine();
spaceLocation=name.indexOf(" ");
firstName=name.substring(0,spaceLocation);
name=name.toUpperCase();
letter=name.charAt(spaceLocation+1);
System.out.print(firstName + " is in Group ");
if(letter>='A' && letter<='I')
System.out.print("1");
else if(letter>='J' && letter<='S')
System.out.print("2");
else
System.out.print("3");
} |
Problem 13 |
public void reverseThatSentence()
{
String sentence,punctuation,wrd="",capital,new_wrd="";
int length,num_spaces=0,num_words=0,counter=0,wrd_length=0;
System.out.print("Enter a sentence: ");
sentence=in.nextLine().trim().toLowerCase();
length=sentence.length();
//store the last character of the sentence - it should be the punctuation
punctuation=sentence.substring(length-1,length);
//count the number of spaces to determine the number of words
for(int i=0; i<length; i++)
{
if(sentence.charAt(i)==' ')
num_spaces++;
}
//declare an array to store the words in the sentence
num_words=num_spaces+1;
String[] words = new String[num_words];
//go through the array starting with the first character.
//add each character onto a variable until a space is encountered
for(int i=0; i<num_words; i++)
{
wrd="";
do{
wrd+=sentence.charAt(counter);
counter++;
}
while(sentence.charAt(counter)!=' ' && counter<length-1);
words[i]=wrd;
counter++;
}
//capitalize first letter in words{num_words-1]
capital=words[num_words-1].substring(0,1).toUpperCase();
// remove the first letter of the word in words[num_words-1]
wrd_length=words[num_words-1].length();
for(int i=1; i<wrd_length; i++)
{
new_wrd+=words[num_words-1].charAt(i);
}
//add capitalized letter to the new word
words[num_words-1]=capital+new_wrd;
//print out the words in the array in reverse order
for(int i=num_words-1; i>=0; i--)
if(i !=0)
System.out.print(words[i]+" ");
else
System.out.print(words[i]);
//add the punctuation stored previously
System.out.println(punctuation);
} |
Problem 14 |
public void vowelsAndConsonants()
{
String sentence;
char letter;
int vowels=0,consonants=0,length,letterUniCode;
System.out.print("Enter a sentence: ");
sentence=in.nextLine().trim().toLowerCase();
length=sentence.length();
for(int i=0; i<length; i++)
{
letter=sentence.charAt(i);
letterUniCode = (int)letter;
if(letterUniCode>=97 && letterUniCode<=122)
{
if(letter=='a' | letter=='i' | letter=='e' | letter=='o' | letter=='u')
vowels++;
else
consonants++;
}
}
System.out.println("Vowels: "+vowels);
System.out.println("Consonants: "+consonants);
} |
Problem 15 |
public void countingSpaces()
{
String sentence,punctuation,wrd="",capital,new_wrd="";
int length,num_spaces=0,pre_spaces=0,post_spaces=0,internal_spaces=0;
System.out.print("Enter a sentence: ");
sentence=in.nextLine();
length=sentence.length();
for(int i=0; i<length; i++){
if(sentence.charAt(i)==' ')
num_spaces++;
}
System.out.println("Total spaces: "+num_spaces );
for(int i=0; i<length; i++){
if(sentence.charAt(i)==' ')
pre_spaces++;
else
break;
}
System.out.println("Spaces before text: "+pre_spaces );
for(int i=length-1; i>=0; i--){
if(sentence.charAt(i)==' ')
post_spaces++;
else
break;
}
System.out.println("Spaces after text: "+post_spaces);
internal_spaces=num_spaces-pre_spaces-post_spaces;
System.out.println("Spaces inside text: "+internal_spaces);
} |
|
|
|