• 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

Reverse the string using only string functions

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can use loops.
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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

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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 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

Ulf Dittmer wrote:In fairness to Suvendu, that was stated exactly in the original post.


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

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic