Solved

swapping string first and last character

Posted on 2014-09-26
2
1,315 Views
Last Modified: 2014-09-28
Hi,

I was trying to swap given string say 'hello' first and last character so that i get output as 'oellh'.

I was checking online and saw below code

int length = s.length();

if (length <= 1) {
    return s;
} else {
    return s.charAt(length - 1) + s.substring(1, length - 1) + s.charAt(0);
}

Open in new window



what is the meaning of ' s.charAt(length - 1) + s.substring(1, length - 1) + s.charAt(0);'

What is the best way to do it. Please advise. thanks in advance
0
Comment
Question by:gudii9
2 Comments
 
LVL 16

Accepted Solution

by:
krakatoa earned 350 total points
ID: 40346650
what is the meaning of ' s.charAt(length - 1) + s.substring(1, length - 1) + s.charAt(0);'

They are the last, middle section, and first characters in a String called s, although if it's a string then it should read s.length() not just length of course.

So the code you saw would be an ok way to do this.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 40347044
One of my previous answers should give you a clue as to how to do this
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What language/protocol is the Angular Chat? 2 77
servlet doXXX methods 3 49
reverse digits of a number using for loop 5 41
MySQL  on Tomcat 8 46
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question