Solved

swapping string first and last character

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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.
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

737 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