Solved

swapping string first and last character

Posted on 2014-09-26
2
1,246 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

912 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now