Question: Write a java program that prompts the user to enter two strings, s1 and s2 , using .nextLine() method and displays the following: 1. longest
Write a java program that prompts the user to enter two strings, s1 and s2, using .nextLine() method and displays the following:
1. longest common prefix of the two strings (case sensitive); 2. number of vowels (A/a, E/e, I/i, O/o, and U/u) in s1; 3. number of consonants in s2; 4. characters of s1 at odd positions; 5. reverse of s2.
If any of Strings s1 or s2 is of length zero then use System.exit(1); to terminate the program. If there is no common prefix, output "N/A" (with quotes) for common prefix.
is a sample run:
| Enter the first string: Welcome to C++ Enter the second string: Welcome to programming Output: 1. The common prefix is: Welcome to 2. # of vowels in s1 is: 4 3. # of consonants in s2 is: 13 4. s1 chars at odd positions: Wloet + 5. reverse of s2 is: gnimmargorp ot emocleW
|
This is what I have so far...however, I am not getting the correct outputs
package hmwk9;
import java.util.Scanner;
public class PE01 { public static void main (String args[]) { // Create a new Scanner object Scanner input = new Scanner(System.in); // Prompt the user to enter two strings System.out.println("Enter the first string: "); String s1 = input.nextLine(); System.out.println("Enter the second string: "); String s2 = input.nextLine(); if (s1.length() == 0 || s2.length() == 0) { System.out.println("Either string 1 or string 2 have a length of 0."); System.exit(0); } else { // Calls methods and displays results char c1 = s1.charAt(0); char c2 = s2.charAt(0); if (c2 == c1) { System.out.println("The common prefix is: " + greatestCommonPrefix(s1,s2)); } else { System.out.println("The common prefix is: \"N/A\""); } System.out.println("# of vowels in s1 is: " + vowelCount(s1)); System.out.println("# of consonants in s2 is: " + countConsonants(s2)); System.out.println("s1 chars at odd positions: " + oddPositions(s1)); System.out.println("Reverse of s2 is: " + reverse(s2)); } input.close(); } public static String greatestCommonPrefix(String s1, String s2) { int minLength = Math.min(s1.length(), s2.length()); for (int i = 0; i < minLength; i++) { if (s1.charAt(i) != s2.charAt(i)) { return s1.substring(0, i); } } return s1.substring(0, minLength); } public static int vowelCount(String s) { int count = 0; int i = 0; //for (int i = 0; i < s.length(); i++) while (i < s.length()) { char c = s.charAt(i); if (c == 'a') { count++; } else if (c == 'e' || c == 'E') { count ++; } else if (c == 'i' || c == 'I') { count++; } else if (c == 'o' || c == 'O' || c == 'u' || c == 'U') { count ++; } else { count = 0; //return count; } } return count; } public static int countConsonants(String s) { int count = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c != 'a' || c != 'A' || c != 'e' || c != 'E' || c != 'i' || c != 'I' || c != 'o' || c != 'O' || c != 'u' || c != 'U' || c != ' ') { count++; } else { count = 0; } } return count; } public static char oddPositions(String s) { for (int i = 0; i < s.length(); i++) { if (i == 0 || ((i % 2) != 0)) { char c = s.charAt(i); System.out.print(c); } } } public static String reverse(String s) { } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
