Question

Java Program : Please save the program with the name ‘Dstring.java’ Write a program that reads...

Java Program : Please save the program with the name ‘Dstring.java’

Write a program that reads a string from the keyboard. If the length of the string is an even number, your program should split the string into two strings of equal length. If the length of the string is odd, your program should split the string into two strings where the first part has one more character than the second part. Your program should output the two strings it created.

For example:

Input:          2BeOrNotToBe

Output:        2BeOrN

                       otToBe

Input:                  ToBeOrNotToBe

Output:           ToBeOrN

                       otToBe

Homework Answers

Answer #1

Here is the code and yeah i made it public so you can save it as "Dstring.java".Hope,my answer helped you.Any firther doubts please feel free to ask us.

  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.Scanner; //Needed for the Scanner class
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Dstring
  7. {
  8. public static void main(String[] args)
  9. {
  10. //to hold the user's input
  11. String intialstr;
  12. String finalstr1=new String();
  13. String finalstr2=new String();
  14.   
  15. //A Scanner object for keyboard input.
  16. Scanner keyboard = new Scanner(System.in);
  17.   
  18. //Prompt the user to enter the string.
  19. System.out.println("Please enter your string: ");
  20. intialstr = keyboard.nextLine();
  21. if((intialstr.length())%2==0){
  22. for(int i=0;i<((intialstr.length())/2);i++){
  23. finalstr1+=intialstr.charAt(i);
  24. }
  25. for(int i=(((intialstr.length()))/2);i<intialstr.length();i++){
  26. finalstr2+=intialstr.charAt(i);
  27. }
  28. }
  29. else{
  30. for(int i=0;i<=((intialstr.length())/2);i++){
  31. finalstr1+=intialstr.charAt(i);
  32. }
  33. for(int i=(((intialstr.length()))/2)+1;i<intialstr.length();i++){
  34. finalstr2+=intialstr.charAt(i);
  35. }
  36. }
  37. System.out.println("String 1="+finalstr1);
  38. System.out.println("String 2="+finalstr2);
  39.   
  40. }
  41. }

Output: (when even)

  1. Please enter your string:
  2. Amarkant
  3. String 1=Amar
  4. String 2=kant

Output:(when odd)

  1. Please enter your string:
  2. Amarkant2
  3. String 1=Amark
  4. String 2=ant2
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a java program that reads two sentences as strings, the program should print the common...
Write a java program that reads two sentences as strings, the program should print the common terms in both sentences example: input: I did my java homework yesterday yesterday i went to java island
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
I need know how to Write a java program called character length that prompts the user...
I need know how to Write a java program called character length that prompts the user to enter a string and displays:x is the first character.) The length of the string is: xxxxx The strings first character is: x (where xxxxx is the length of the string and x is the first character.)
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
A Simple Parenthesis Checker (ParenChecker) Write a Java application that checks a string for correct parenthesization....
A Simple Parenthesis Checker (ParenChecker) Write a Java application that checks a string for correct parenthesization. The input comes from the keyboard and should (but might not) consist solely of the delmiters (, ), [, ], {, and } separated by whitespace. The output of the program is one of the following: OK Opener/closer mismatch Missing closer Missing opener Unexpected symbol The name of your class should be ParenChecker. The reasonable thing to do here is to use a stack...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
Write a program that reads an integer, a list of words, and a character. The integer...
Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle z then...
Write a Java method that takes a String as Input and replaces the first two occurrencesof...
Write a Java method that takes a String as Input and replaces the first two occurrencesof a given character to another provided character.   For example, if the input String is “Java is Great” and asked to replace the letter a with x then the output would be: “Jxvx is Great” The method takes a String and two characters and returns the modified String. Please make your own assumptions if needed, you do not need to write the main.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT