5 – String Answers

1) Output the length of the String “I never saw a purple cow”

String s = "I never saw a purple cow";
System.out.println(s.length());

2) Convert the String “I never saw a purple cow” to uppercase and output the resulting string.

String s = "I never saw a purple cow";
String u = s.toUpperCase();
System.out.println(u);

3) Output the String “I never saw a purple cow” as separate words.

String s = "I never saw a purple cow";
String [] words = s.split("\\s+");
for(String w:words)
{
    System.out.println(w);
}

4) Output the following String array as one String: words [] = {“Calling”, “occupants”, “of”, “interplanetary”, “craft”};

String s = "";
String [] words = {"Calling", "occupants", "of", "interplanetary", "craft"};
for(String w:words)
{
    s = s + w + " ";
}
System.out.println(s);

5) Prompt the user to enter a string. Output the string as separate words in alternate upper and lower case: SO it LOOKS like THIS example

boolean isUpper = true;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
String [] words = s.split("\\s+");
String sentence = "";
for(String word: words)
{
    if(isUpper)
    {
    sentence = sentence + word.toUpperCase() + " ";
    }
    else
    {
    sentence = sentence + word.toLowerCase() + " ";
    }
    isUpper = !isUpper;
}
System.out.println(sentence);

6) Prompt the user to enter a String. Output a String named acronym that contains the initial letters of the words input. Example: input “British Broadcasting Corporation” output “BBC”

Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
String [] words = s.split("\\s+");
String acronym = "";
for(String word: words)
{
    acronym = acronym + word.substring(0,1).toUpperCase();
}
System.out.println(acronym);

7) Prompt the user to enter a string. Output the number of times the letter ‘e’ appears in the string.

Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
int count = 0;
for(int i = 0; i < s.length(); i++)
{
    if(s.charAt(i) == 'e') count++;
}
System.out.println("Number of times e appears: " + count);

8) Prompt the user to enter a string. Output the number of vowels in the String.

Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
int count = 0;
for(int i = 0; i < s.length(); i++)
{
    if(s.substring(i,i+1).matches("[aeiouAEIOU]")) count++;
}
System.out.println("Number of times vowel appears: " + count);

9) Prompt the user to enter a String. Output a String with the vowels replaced with *’s. Example: input “I never saw a purple cow” output “* n*v*r s*w * p*rpl* c*w”

Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
System.out.println(s.replaceAll("[AEIOUaeiou]", "*"));

10) A palindrome is a string that reads the same forwards and backwards. Examples are “radar” and “rotavator”. Write a program that accepts a String as input and outputs “Palindrome” if the String is a palindrome, and “Not Palindrome” otherwise.

Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
String reverse= "";
for (int i = 0; i<s.length(); i++)
{
    reverse = s.substring(i,i+1) + reverse;
}
if (s.equals(reverse))
{
    System.out.println("Palindrome");
}
else
{
    System.out.println("Not Palindrome");
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.