Solved

swapping string first and last character

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

12 Experts available now in Live!

Get 1:1 Help Now