Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reverse string in java

    for my java class i need to write a method that would take a string the user inputs and output the characters in reverse order. Any ideas on how to do this cause i have no clue. Thanks

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don't know java, but the baisc process woulmd be this

    -make an array the size of the length of your string
    -make a for loop iterating up(++) to the strings length (lets call it i)
    -take originalString[i] and place it in newString[originalString-i]

    that should do it
    photoshop too expensive? use the GIMP! www.gimp.org

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Yup, do what Whackaxe suggested, or you could always just use charAt() and make a String instead of an array. Haven't tried it, but it should work.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could do it without the array, i'd write it like this in javascript, would be damn similar in java:

    Code:
        var result;
        result = '';
        for (var position = 0; position < aString.length; position = position + 1)
        {
            result = aString.charAt(position) + result
        };
        return result

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by KeZZeR
    You could do it without the array, i'd write it like this in javascript, would be damn similar in java:

    Code:
        var result;
        result = '';
        for (var position = 0; position < aString.length; position = position + 1)
        {
            result = aString.charAt(position) + result
        };
        return result
    You realize you just did his homework for him right?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot for the help ive been working on this all week and i couldnt figure it out. So now my next problem is i have to make a method that takes a string and removes the vowels then returns the string. Any Ideas?
    Last edited by cwl157; 11-11-2004 at 01:08 AM.

  • #7
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Couple ways to do this, but I would recommend using the stringbuffer. These objects are made for modifying strings. Just create an array of vowel characters and make your comparisons. Loop through the stringbuffer and check to see if the character matches any index in the vowel array, if yes, delete it, if no, move on. Later.

  • #8
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Loop through the string changing each character to an int. Then check using the dec. value it returns. It's easier if you convert it to all lower case. If the character does not fall into one of the ranges you set, add it to a new string.

    That's probably more of the way they want it done. If you want it done quick and easy look through here.
    Last edited by squirellplaying; 11-11-2004 at 11:43 AM.

  • #9
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by cwl157
    Thanks a lot for the help ive been working on this all week and i couldnt figure it out. So now my next problem is i have to make a method that takes a string and removes the vowels then returns the string. Any Ideas?
    lol, i had to do the exact same thing back in my old course. What/where are you doing this for study?

  • #10
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thos 2 solutions sound a little complicated for my current level?

  • #11
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It doesn't get any easier then that. Read the second link I gave you. That is the similest that it gets.

  • #12
    New Coder
    Join Date
    Jun 2003
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i figured out the program and everything works to remove the vowels i just used an if statement that compared each char in the string to every different vowel and if they were not equal then it added that char to the final string.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •