1) Output the length of the String "I never saw a purple cow"
```java
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.
```java
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.
```java
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"};
```java
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
```java
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 initialism that contains the initial letters of the words input. Example: input "British Broadcasting Corporation" output "BBC"
```java
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = scan.nextLine();
String \[\] words = s.split("\\\\s+");
String initialism = "";
for(String word: words)
{
initialism = initialism + 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.
```java
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.
```java
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"
```java
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.
```java
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");
}
```