Given a String, return an array of char values that contains the characters of the string in reverse order...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-3















From the title you can see what I'm trying to do, however it's not working and I'm not sure why. Who has any ideas or simpler way to do this method?
(without using string builder and all that extra stuff)



public char reverseStringToArray(String str)
{

char reverseStringToArray = str.toCharArray();

char characters = new char[reverseStringToArray.length];

for (int i = reverseStringToArray.length - 1; i >= 0; i--)

characters[i] = reverseStringToArray[i];

return reverseStringToArray;
}









share|improve this question















closed as off-topic by Hovercraft Full Of Eels, cнŝdk, Unheilig, EdChum, aaaaaa123456789 Nov 22 '18 at 9:59


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Unheilig, aaaaaa123456789

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, cнŝdk, EdChum


If this question can be reworded to fit the rules in the help center, please edit the question.












  • 2





    Here characters[i] = reverseStringToArray[i]; those indices should be different. I'll leave it to you to figure out what they should be, but it's very simple math.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 4





    Also this return reverseStringToArray[i]; shouldn't compile!

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 2





    Side question -- why are you using Character and not char? A Character array is not the same as a char array.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:40




















-3















From the title you can see what I'm trying to do, however it's not working and I'm not sure why. Who has any ideas or simpler way to do this method?
(without using string builder and all that extra stuff)



public char reverseStringToArray(String str)
{

char reverseStringToArray = str.toCharArray();

char characters = new char[reverseStringToArray.length];

for (int i = reverseStringToArray.length - 1; i >= 0; i--)

characters[i] = reverseStringToArray[i];

return reverseStringToArray;
}









share|improve this question















closed as off-topic by Hovercraft Full Of Eels, cнŝdk, Unheilig, EdChum, aaaaaa123456789 Nov 22 '18 at 9:59


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Unheilig, aaaaaa123456789

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, cнŝdk, EdChum


If this question can be reworded to fit the rules in the help center, please edit the question.












  • 2





    Here characters[i] = reverseStringToArray[i]; those indices should be different. I'll leave it to you to figure out what they should be, but it's very simple math.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 4





    Also this return reverseStringToArray[i]; shouldn't compile!

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 2





    Side question -- why are you using Character and not char? A Character array is not the same as a char array.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:40
















-3












-3








-3


1






From the title you can see what I'm trying to do, however it's not working and I'm not sure why. Who has any ideas or simpler way to do this method?
(without using string builder and all that extra stuff)



public char reverseStringToArray(String str)
{

char reverseStringToArray = str.toCharArray();

char characters = new char[reverseStringToArray.length];

for (int i = reverseStringToArray.length - 1; i >= 0; i--)

characters[i] = reverseStringToArray[i];

return reverseStringToArray;
}









share|improve this question
















From the title you can see what I'm trying to do, however it's not working and I'm not sure why. Who has any ideas or simpler way to do this method?
(without using string builder and all that extra stuff)



public char reverseStringToArray(String str)
{

char reverseStringToArray = str.toCharArray();

char characters = new char[reverseStringToArray.length];

for (int i = reverseStringToArray.length - 1; i >= 0; i--)

characters[i] = reverseStringToArray[i];

return reverseStringToArray;
}






java arrays string loops char






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 1:56







whose mans is this

















asked Nov 22 '18 at 1:38









whose mans is thiswhose mans is this

11




11




closed as off-topic by Hovercraft Full Of Eels, cнŝdk, Unheilig, EdChum, aaaaaa123456789 Nov 22 '18 at 9:59


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Unheilig, aaaaaa123456789

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, cнŝdk, EdChum


If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Hovercraft Full Of Eels, cнŝdk, Unheilig, EdChum, aaaaaa123456789 Nov 22 '18 at 9:59


This question appears to be off-topic. The users who voted to close gave these specific reasons:



  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Unheilig, aaaaaa123456789

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Hovercraft Full Of Eels, cнŝdk, EdChum


If this question can be reworded to fit the rules in the help center, please edit the question.








  • 2





    Here characters[i] = reverseStringToArray[i]; those indices should be different. I'll leave it to you to figure out what they should be, but it's very simple math.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 4





    Also this return reverseStringToArray[i]; shouldn't compile!

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 2





    Side question -- why are you using Character and not char? A Character array is not the same as a char array.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:40
















  • 2





    Here characters[i] = reverseStringToArray[i]; those indices should be different. I'll leave it to you to figure out what they should be, but it's very simple math.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 4





    Also this return reverseStringToArray[i]; shouldn't compile!

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:39






  • 2





    Side question -- why are you using Character and not char? A Character array is not the same as a char array.

    – Hovercraft Full Of Eels
    Nov 22 '18 at 1:40










2




2





Here characters[i] = reverseStringToArray[i]; those indices should be different. I'll leave it to you to figure out what they should be, but it's very simple math.

– Hovercraft Full Of Eels
Nov 22 '18 at 1:39





Here characters[i] = reverseStringToArray[i]; those indices should be different. I'll leave it to you to figure out what they should be, but it's very simple math.

– Hovercraft Full Of Eels
Nov 22 '18 at 1:39




4




4





Also this return reverseStringToArray[i]; shouldn't compile!

– Hovercraft Full Of Eels
Nov 22 '18 at 1:39





Also this return reverseStringToArray[i]; shouldn't compile!

– Hovercraft Full Of Eels
Nov 22 '18 at 1:39




2




2





Side question -- why are you using Character and not char? A Character array is not the same as a char array.

– Hovercraft Full Of Eels
Nov 22 '18 at 1:40







Side question -- why are you using Character and not char? A Character array is not the same as a char array.

– Hovercraft Full Of Eels
Nov 22 '18 at 1:40














1 Answer
1






active

oldest

votes


















2














StringBuilder has a useful method reverse(), you can use that and then convert to a char. Like,



return new StringBuilder(str).reverse().toString().toCharArray();


If you must implement your own array reverse here, it's important to know not to reverse elements beyond the half-way mark (why? because you will reverse them and then reverse them back). Note that you also need to perform a full swap or you'll lose half of your input. That is, it should look something like,



char rsa = str.toCharArray();
for (int i = 0; i < rsa.length / 2; i++) {
char ch = rsa[i];
rsa[i] = rsa[rsa.length - i - 1];
rsa[rsa.length - i - 1] = ch;
}
return rsa;





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    StringBuilder has a useful method reverse(), you can use that and then convert to a char. Like,



    return new StringBuilder(str).reverse().toString().toCharArray();


    If you must implement your own array reverse here, it's important to know not to reverse elements beyond the half-way mark (why? because you will reverse them and then reverse them back). Note that you also need to perform a full swap or you'll lose half of your input. That is, it should look something like,



    char rsa = str.toCharArray();
    for (int i = 0; i < rsa.length / 2; i++) {
    char ch = rsa[i];
    rsa[i] = rsa[rsa.length - i - 1];
    rsa[rsa.length - i - 1] = ch;
    }
    return rsa;





    share|improve this answer






























      2














      StringBuilder has a useful method reverse(), you can use that and then convert to a char. Like,



      return new StringBuilder(str).reverse().toString().toCharArray();


      If you must implement your own array reverse here, it's important to know not to reverse elements beyond the half-way mark (why? because you will reverse them and then reverse them back). Note that you also need to perform a full swap or you'll lose half of your input. That is, it should look something like,



      char rsa = str.toCharArray();
      for (int i = 0; i < rsa.length / 2; i++) {
      char ch = rsa[i];
      rsa[i] = rsa[rsa.length - i - 1];
      rsa[rsa.length - i - 1] = ch;
      }
      return rsa;





      share|improve this answer




























        2












        2








        2







        StringBuilder has a useful method reverse(), you can use that and then convert to a char. Like,



        return new StringBuilder(str).reverse().toString().toCharArray();


        If you must implement your own array reverse here, it's important to know not to reverse elements beyond the half-way mark (why? because you will reverse them and then reverse them back). Note that you also need to perform a full swap or you'll lose half of your input. That is, it should look something like,



        char rsa = str.toCharArray();
        for (int i = 0; i < rsa.length / 2; i++) {
        char ch = rsa[i];
        rsa[i] = rsa[rsa.length - i - 1];
        rsa[rsa.length - i - 1] = ch;
        }
        return rsa;





        share|improve this answer















        StringBuilder has a useful method reverse(), you can use that and then convert to a char. Like,



        return new StringBuilder(str).reverse().toString().toCharArray();


        If you must implement your own array reverse here, it's important to know not to reverse elements beyond the half-way mark (why? because you will reverse them and then reverse them back). Note that you also need to perform a full swap or you'll lose half of your input. That is, it should look something like,



        char rsa = str.toCharArray();
        for (int i = 0; i < rsa.length / 2; i++) {
        char ch = rsa[i];
        rsa[i] = rsa[rsa.length - i - 1];
        rsa[rsa.length - i - 1] = ch;
        }
        return rsa;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 1:54

























        answered Nov 22 '18 at 1:43









        Elliott FrischElliott Frisch

        156k1396191




        156k1396191

















            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)