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
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...
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...
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...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
Please code in Java Write a recursive method that takes a string and return a string...
Please code in Java Write a recursive method that takes a string and return a string where every character appears twice. For example, if the string is “HELLO”, it will return “HHEELLOO”. Write a program to test it.
Write a Python program that splits, doubles, and reverse each input string entered. For each string,...
Write a Python program that splits, doubles, and reverse each input string entered. For each string, split it at the given integer, and then output it in format as shown below. Convert all stings entered to uppercase for output. Assume string length is greater than the split value. Refer to the sample output below. Sample Runs: Enter a string: holiday Enter the number to split on: 3 HOL-YADI-LOH-IDAY Enter a string: TATTARRATTAT Enter the number to split on: 6 TATTAR-TATTAR-RATTAT-RATTAT...
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
Write a Python program that reads in a string and determines whether the string is a...
Write a Python program that reads in a string and determines whether the string is a palindrome or not. note that a sentence that reads the same forwards and backwards by resequencing the spaces is also a palindrome and your program should consider this. sample run enter string: mom output: mom is a polindrame enter string: a nut for a jar of tuna output: a nut for a jar of tuna is a palindrome
Write a simple TCP program for a client that reads a line of text from its...
Write a simple TCP program for a client that reads a line of text from its standard input (keyboard) and sends the line out its socket to the server. The server reads the line from the socket and prints it in its standard output (screen). The server then reads a line from its standard input (keyboard) and sends it out its socket to the client. When the client sends “!” the server should reply back “bye” to the client and...