• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Reverse String Method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to add a method to MyStringClass to reverse the string without
importing the Java string library?

 
Bartender
Posts: 2698
130
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to coderanch!
Use code tags whenever you paste code. This allows your post to be more readable. I have gone ahead and done it for you this time.
 
Rancher
Posts: 218
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to make a new String based off the old one. Since you need to capture every letter and reverse its position, I would recommend using some sort of loop. A for loop or a for each loop would likely be good choices. Some valuable methods you might want to use on the String is str.charAt(x) which retrieves the letter at x position, and str.substring(x,y) which takes a substring of the str from x to y.

One way of doing it would be the following:
- Start a loop from the length of the string - 1 (which is the end of the string)
- Get the character at the loop position
- Append that character onto a new String
- Continue loop going backwards until you reach the i < 0 iteration.
 
salvin francis
Bartender
Posts: 2698
130
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Imamu Onyeka wrote:How to add a method to MyStringClass to reverse the string without
importing the Java string library?


The String class does not need to be imported since it's a part of lang package. It's not a library.
Maybe you are referring to the reverse method in StringBuilder or StringBuffer.

If yes, where have you got this question from ? Is it your homework for some problem ?
 
salvin francis
Bartender
Posts: 2698
130
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zachary Griggs wrote:...Append that character onto a new String...


Wouldn't it be simpler to run a simple loop and prepend instead ?
 
Marshal
Posts: 70654
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's any appending or prepending to be done, use a StringBuilder (which you don't need to import either).

And, again, welcome to the Ranch
 
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
If you don’t want you to use methods of the String class, then you will have to use its peer class, namely the StringBuilder class, which has two methods, namely


public StringBuilder reverse(String Str)
String toString()
and a constructor, namely

StringBuilder(String Str)

charAt function can be used.

Using all these four you can reverse your string (without using any String class method)

I hope this will help to you
 
Rancher
Posts: 912
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imamu,

Generally speaking the string reversal assignment is given when the teacher it talking about recursion.  It is a classic and very simple exercise to reverse a string using recursion.  If the instructor was taking about recursion when he/she gave the assignment, I strongly suggest you use recursion to complete the assignment.

Les
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic