• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reverse the string using only string functions  RSS feed

 
SuvenduSekhar Panda
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am doing some stuff where I need to reverse a string i.e

input should be like this- "My name is Jitun" and
output should be - "Jitun is name my"

to do the above task i can do it using arrays but i am not getting any idea how to do it using string functions . can any one help me for this.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, you should KeepItDown - it looks like you're shouting at us. Please edit the subject of your post accordingly.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String class has a method that splits a string as determined by some specific delimiter (like a space character). That would again lead to array handling, but there's nothing wrong with that, is there? (Or if there is, you should tell us why.)
 
SuvenduSekhar Panda
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No,but
I mean to say without using (String[]) type of concept. To take the values and store it in the defined array and use it by taking from that array.
Using only String functions. Is it possible to do?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it could be possible via some looping. Are we allowed to use loops? You said only String functions...
 
SuvenduSekhar Panda
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can use loops.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick solution can be to use StringBuilder object

Sample code


Try this. It may solve your query.
 
SuvenduSekhar Panda
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhay,

It gives the simple reverse operation. My requirement is to reverse the string but not the words in them. i.e

input is like-"My name is Jitun" and
output should be -"Jitun is name My"


Thanks & regards

Suvendu
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SuvenduSekhar Panda wrote:My requirement is to reverse the string but not the words in them.

Then you should have told us that at the start. Learning how to ask questions is very important, otherwise we can spend a lot of time on things that don't help you.

So, if you're not allowed to use String.split() (which IS a String function), then you will have to determine when a word break occurs. Just one way would be to store the indexes of every space in your string (assuming that "words" can only be broken up with spaces), and then use that array of values to create (or print()) substring()s in reverse sequence.

You may also want to have a look at String.indexOf(char, int) and String.lastIndexOf(char, int) to help you out.

HIH

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is to reverse the string but not the words in them.

Then you should have told us that at the start.

In fairness to Suvendu, that was stated exactly in the original post.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:In fairness to Suvendu, that was stated exactly in the original post.

Oops; you're quite right. My apologies, Suvendu.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!