Question

In: Computer Science

Given a string of at least 3 characters as input, if the length of the string...

Given a string of at least 3 characters as input, if the length of the string is odd return the character in the middle as a string. If the string is even return the two characters at the midpoint.

--------------------------------------------------------------

public class Class1 {

public static String midString(String str) {
    //Enter code here
}
}

Solutions

Expert Solution

Please find the Updated code for the following:

Code:

public class Class1
{
   //Defined a method which accepts a string and returns a string
   public static String midString(String str)
   {
  
       //Check if the string is an odd length
if(str.length()%2==0)
{
//Then returns the two characters at the midpoint
   return str.substring(str.length()/2-1,str.length()/2+1);
}
//Else part where the string is an even length
else
{
//Then return the character in the middle
   return str.substring(str.length()/2,str.length()/2+1);
}
  
}
   //Validating the above method
   public static void main(String[] args)
   {
       //Defined 2 strings one is of even and other is off odd length
       String EvenLengthString="ABCDEFGH";
       String oddLengthString ="RSTUVWXYZ";
       //Calling the function and then printing out the resultant string
       System.out.println(midString(EvenLengthString));
       System.out.println(midString(oddLengthString));
   }
  
}

Please check the compiled program and its output for your reference:

Output:

(Feel free to drop me a comment, If you need any help)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

Java:    Find a pattern that will match any string that is --  at least 6 characters...
Java:    Find a pattern that will match any string that is --  at least 6 characters long, -- and begins with a letter or number (\w) -- and contains at least one non-letter and non-number (\W).
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
/**    * Returns the string formed by alternating the case of the characters in   ...
/**    * Returns the string formed by alternating the case of the characters in    * the specified string. The first character in the returned string is in    * lowercase, the second character is in uppercase, the third character is    * in lowercase, the fourth character is in uppercase, and so on.    * Examples:    *    * <ul>    * <li><code>alternatingCaps("a")</code> returns <code>"a"</code>    * <li><code>alternatingCaps("ab")</code> returns <code>"aB"</code>    * <li><code>alternatingCaps("abc")</code> returns <code>"aBc"</code>    *...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
In this Exercise, you have to take a single string as input. Using this input string,...
In this Exercise, you have to take a single string as input. Using this input string, you have to create multiple queues in which each queue will comprise of separate word appeared in input string. At the end, you will again concatenate all queues to a single queue.s Example: String = “Data Structure and Algo” Q1 = D → a → t → a Q2 = S → t → r → u → c → t → u →...
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT