Question

In: Computer Science

Write Java program Lab52.java which reads in a line of text from the user. The text...

Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method:

  • public static String[] divideText(String input)

The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string.

The program should then print out the returned strings.

Solutions

Expert Solution

Explanation:

Here is the code which has the method divideText which takes the string as input and then all the odd index characters are put together in one string and the even index characters are put together in another string.

Then forming an array of these strings, it is returned to main method.

Code:

import java.util.Scanner;

public class Main
{
public static String[] divideText(String input)
{
String even_str = "", odd_str = "";
  
for(int i=0; i<input.length(); i++)
{
if(i%2==0)
even_str = even_str + input.charAt(i);
else
odd_str = odd_str + input.charAt(i);
}
  
String arr[] = {even_str, odd_str};
  
return arr;
}
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       String line = sc.nextLine();
      
       String arr[] = divideText(line);
      
       System.out.println(arr[0]);
       System.out.println(arr[1]);
   }
}

output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Write a program named StringWorks.java that asks the user to input a line of text from...
Write a program named StringWorks.java that asks the user to input a line of text from the keyboard.   Ask the user if they want their answers case sensitive or not. You output should be the list of words in the sentence including duplicates A sorted list of the words (alphabetically) A sorted list of words listed backwards (where z comes before a) A randomly shuffled list of works the list of words in the sentence alphabetically removing duplicates. You need...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
Write a complete Python program that asks the user for a line of text (not just...
Write a complete Python program that asks the user for a line of text (not just a single word), and then tells the user whether the string is a palindrome (same forward and backward) if you ignore case and non-alphabetic characters. The following methods and functions may be useful: str.upper(), str.isalpha() -> bool, reversed(str) -> sequence, str.find(str) -> int, str.replace(str, str), str.center(int). Unless otherwise specified, they all return a string.
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression...
Write a JAVA program that reads a text file into RAM efficiently, takes a regular expression from the user, and then prints every line that matches the RE.
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase,...
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). Please use a Queue Class and Stack class.
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT