• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

swapping string first and last character

 
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to swap given string first and last character.
If I pass world i should get 'dorlw'

I found below link and code
http://www.wikihow.com/Manipulate-Strings-in-Java




I wonder Is there is any easy simple way to do it. Please advise

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can be done in one line - maybe two if you want it a bit more readable.
Have a look at the charAt and substring methods of the String class.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:
I found below link and code
http://www.wikihow.com/Manipulate-Strings-in-Java



If the goal is to learn, and since this is a simple example, perhaps it would be a good idea to start from scratch -- and trying doing it yourself. You lose a lot of learning by starting from someone else's code, and probably less by having a forum improve it for you.

Henry
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Starting from scratch and do completely myself sometimes taking too long time and I feel I am not going any where during that thinking or idle time. Even after few hours and days i am not able to progress sometimes. the time i have to practice also limited say few hours a day. Please advise

I wrote as below



Which is reducing number of lines of code and seems more simple.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Which is reducing number of lines of code and seems more simple.


Well done. You even remembered to check for strings of less than two characters which I had forgotten to do.
The only change I would make is to take out the if statement on line 6. You already know the string length is greater than 1 from the check you did on line 2.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Remove second if (line 6) and then use else instead as above?
Please advise
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by reading the full descriptions of the String methods, charAt and substring.
Then work out whether the if-else is necessary. You will of course have problems if you pass a 0‑length String as an argument.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Remove second if (line 6) and then use else instead as above?
Please advise


You can have an else if you want but it's not needed. If the condition is true you return from the method, so, with or without the else, the last line will only ever be executed if the condition is false.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You will of course have problems if you pass a 0‑length String as an argument.


Why ?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you miss out the if-else and try substring(1) no an empty String, you will suffer an Exception.
I think the if-else is unnecessary, as long as you never have a 0‑length String.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you miss out the if-else and try substring(1) no an empty String, you will suffer an Exception.


Okay, but sai wasn't talking about removing the if, just the else.
The if he was refering to was the second one in his previous post which I had told him wasn't necessary.
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So without else allowing natural flow to reach line --->return s.charAt(length - 1) + s.substring(1, length- 1)+s.charAt(0);
as above code is sufficient right?
But seeing two return statements in a method one inside if loop and other outside the if loop as last line of the method looks bit odd to me
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn! It doesn't work. I was mistaken. I tried it with isEmpty and got an exception for a 1‑length String. You will have to change it.Still no need for an else.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:But seeing two return statements in a method one inside if loop and other outside the if loop as last line of the method looks bit odd to me


It's not odd really, it's just a matter of recognition. I treat "no multiple returns in a method" as a guideline rather than an inviolable rule. I use the guiding principle of Code Clarity and Simplicity to decide whether or not to apply No Multiple Returns. I actually prefer writing short methods that use guard clauses rather than forcing the use of a temporary variable just so I don't violate No Multiple Returns. Martin Fowler's refactoring to guard clauses example actually eliminates the result temporary variable and ends up with four return statements in a single short method: http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html


 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could not resist posting a Scala version!

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree the Martin Fowler version is easier to read than that above.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Harry wrote:I could not resist posting a Scala version!


Have you tried this with:

val list = "w"

?

And what is wrong with: val result = word.last +: word.tail.init :+ word.head?

Greetz,
Piet

edit: you do not need the "world".toList. The string is kind of autoboxed to a list of chars.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:I am trying to swap given string first and last character.
If I pass world i should get 'dorlw'


Then you might ask yourself why you have a loop at all.

Most of the rest of this thread (and the code in your example) seems to be about reversing a String - a much more complex operation - when your question, and your example result, are NOT.

However, if this discussion has indeed moved on to one about reversing, then it may be worth pointing out that for loops are NOT limited to a single variable, for example:

for (i = 1, j = 10; i < someValue; i++, j += 10) { ...

HIH

Winston

[Edit: It's been pointed out that I'm wrong, but I've left the post because I think the point about variables is worth remembering.]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Agree the Martin Fowler version is easier to read than that above.


For me it goes a bit deeper than just readability. The motivation for the refactoring is to make it clear which ones are the exceptional cases vs. the normal case. In this code pattern, guard clauses indicate the exceptional cases and anything beyond the guard clause(s) is the normal case. When you understand and are familiar with the pattern and the motivation, constructs such as the nested ternary expression are just a little bit "too clever" and take away from the concept that the pattern tries to clarify. Again, the guiding principle is Simplicity and Clarity.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree, Junilu.

Shall we get back to swapping ends of Strings?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Shall we get back to swapping ends of Strings?


I thought the OP already got it in the fourth post in this thread.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jelle Klap wrote:[code]public String swapEnds(String value) { ...


What about value == null?

Winston
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't accept null, because value is not annotated with @Nullable, so a NPE is to be expected
 
sai rama krishna
Ranch Hand
Posts: 930
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I was checking refactoring website and found code as above why there ';' at the end after }
Is it is inner class or a method? Please advise

I see above code is written as below which is more readable, simple

I wonder what is the meaning of 'guard clause'
Please advise
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:

And what is wrong with: val result = word.last +: word.tail.init :+ word.head?



Even better!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic