Question

I need some help on Java. I'm stuck on trying to generate a new string by...

I need some help on Java. I'm stuck on trying to generate a new string by concatenating the reversed substrings of even indexes and odd indexes separately from a given string. I'm not sure how to figure this out. See example.

Example:

Input: abscacd

Output: dasaccb

Substrings: asad, bcc

Reversed substrings: dasa, ccb.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class ReverseSubStrings {
public static void main(String[] args) {
  
   // reading string from user
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter String");
   String str=sc.nextLine();
  
   String even="";
   String odd="";
// looping to get even and odd substrings
   for(int i=0;i<str.length();i++){
       if(i%2==0)
           even=even+str.charAt(i)+"";
       else
           odd=odd+str.charAt(i)+"";
   }
   // reversing the sub strings
   String rev1=new StringBuffer(even).reverse().toString();
   String rev2=new StringBuffer(odd).reverse().toString();
   System.out.println("Reversed substrings : "+rev1+","+rev2);
   System.out.println("Output : "+rev1+rev2);
}
}

Add a comment
Know the answer?
Add Answer to:
I need some help on Java. I'm stuck on trying to generate a new string by...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /*************************************/ I'm new to java and, I am trying to learn the biFunction in java to...

    /*************************************/ I'm new to java and, I am trying to learn the biFunction in java to see if string x is in string y are equal.        BiFunction (String x, String y)-> { return true; }; I know I want to end it with a return statement, but not sure how to compare the two without bugs

  • I need some help with Java. I need to write a Java program that generates a...

    I need some help with Java. I need to write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and character belong to the original string. See example. Example Input: “Hello World” (length 9) Output: “World oHlel” (length 9)

  • Need help in Java network,    I'm trying to pass Junit 4 tests.       my...

    Need help in Java network,    I'm trying to pass Junit 4 tests.       my Code: public Character findIt( String url){        try {               new URL("myurl").toURI();            System.out.println("pass");            } return null;    } Test case:    @Test    public void testForValidOrNot(){               ValidOrNot validOrNot = new ValidOrNot();                       assertEquals( new Character('pass'), validOrNot.findIt( "my url" ) );    Trying to see why...

  • I'm trying to recreate an experiment from class but I'm missing some code. I need an...

    I'm trying to recreate an experiment from class but I'm missing some code. I need an arduino uno program that will ask the user to enter a duty cycle and frequency, and to then generate a square pulse width (0-5V) based on those values.

  • I need help and I'm stuck on this program. I have to do it in the...

    I need help and I'm stuck on this program. I have to do it in the C language on my Linux system. Can someone help me with this please? I have included my instructions and sample output. Thank you -Initialize 2 arrays within the main method, one with odd numbers and one with even numbers. -In the main method swap the arrays. -Print the arrays before and after the swap. //output should look like the following Before Swap firstArray[0] =...

  • I'm really stuck I need help. C++ Draw separate flowcharts to solve each of the following...

    I'm really stuck I need help. C++ Draw separate flowcharts to solve each of the following problems: Read in 20 numbers and write out how many of them were positive (write out the count, not the actual input numbers). Read in numbers until the user chooses to quit. Write out the smallest of the input values. (Make it so that the user has to enter at least one value) Read in numbers until 10 positive values have been found. Write...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • I'm trying to construct this program in C#, I found some similiar problems in Java but...

    I'm trying to construct this program in C#, I found some similiar problems in Java but as a beginner, its not very easy to follow along with the Java code. Create a Console application named RelativesApp to help you remember your relatives’ birthdays. ·Create a Relative class that includes auto-implemented properties for the relative's name, their relationship to you, and two integers to represent their birthday –month, day. ·Declare an array of at least 12 relative objects. ·Fill the array...

  • I'm still fairly new to java and I haven't programmed with it in half a year,...

    I'm still fairly new to java and I haven't programmed with it in half a year, so I can't for the life of me figure this out even though I know its really simple. I'll paste the 2 classes below so any help is much appreciated. The TODO comments are what I'm supposed to do. public class Main { public static void main(String[] args) { /* * This is for the first part of the lab. */    Planner coursePlan...

  • Any ideas. I'm stuck and need help A 20Ohm coil of 15 turns and radius 10.0...

    Any ideas. I'm stuck and need help A 20Ohm coil of 15 turns and radius 10.0 cm surrounds a long solenoid of radius 5.0 cm and 1000 turns/meter (see figure below). The current in the solenoid changes as I = b sin (a t), where I is in amperes and t is in seconds. Let a = 120 rad/s and b = 6 A. What is the magnitude of the EMF in the 15-turn coil as a function of time?...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT