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)

  • Can someone please help me? I'm having trouble with this assignment and have been stuck trying...

    Can someone please help me? I'm having trouble with this assignment and have been stuck trying to figure it out for over an hour. Here's the assignment details below: For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents...

  • 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...

  • Hey, need some help in Java. I'm stuck on one step that prevents me from doing...

    Hey, need some help in Java. I'm stuck on one step that prevents me from doing the rest of the steps and need help on it. I also have a problem when I print databaseCourse and programmingCourse. instead of saying 5 and 7 respectively is shows 51 and 71. Step 8: in CourseGrades, create a method, add, that takes two parameters, studentNum and grade, and changes the grade of student studetNum to the given grade, grade. studentNum represents the student...

  • 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...

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