?
Solved

swapping string first and last character

Posted on 2014-09-26
2
Medium Priority
?
1,576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
krakatoa earned 1400 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 600 total points
ID: 40347044
One of my previous answers should give you a clue as to how to do this
0

Featured Post

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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