In: Computer Science
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method:
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.
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!