reading strings..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carly
    New Member
    • Oct 2006
    • 37

    reading strings..

    Hi Everyone,

    I have been trying to do this question but i dont really know how to start.

    - Write a program which will read a string and rewrite it alphabetical order. For example, the word STRING should be written as GINRST.

    I did this code, but im having some trouble finishing it off, especially with this part (STRING should be written as GINRST).

    Code:
    Public static void main (String[]args){
    InputStreamReader tr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader (in);
    String s = new String();

    thanks in advance,
    carly
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by carly
    Hi Everyone,

    I have been trying to do this question but i dont really know how to start.

    - Write a program which will read a string and rewrite it alphabetical order. For example, the word STRING should be written as GINRST.

    I did this code, but im having some trouble finishing it off, especially with this part (STRING should be written as GINRST).

    Code:
    Public static void main (String[]args){
    InputStreamReader tr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader (in);
    String s = new String();

    thanks in advance,
    carly
    You need to take the input as
    Code:
    String s = br.readLine();
    Convert the string to array of characters using

    Code:
    char[] chars = s.toCharArray();
    Sort the char[] using any sort method you like.

    Convert the char[] back to string using

    Code:
    s = new String(chars);

    Comment

    • kotoro
      New Member
      • Nov 2006
      • 32

      #3
      the method given by the previous answer is probably much more efficient than my answer is going to be, but if this is for a programming class, they probably don't want you to use built in sorts and toCharArray

      you could always create a new string and append (and save the resulting new string reference over the old string) the characters as you work backwards in the index of the input string.

      I'm assuming you're allowed to use loops.
      for loops are while loops with built in spaces for operations to be run at the start of the loop and at the end of each pass.

      to interpret my loop if you haven't been taught the use of "for" loops, you can convert it to a while loop.
      Code:
      For( (this spot executes at the start) ; (This is condition for continuing the loop) ; (this executes before checking the condition) )
      Code:
      //assume input of String s, I dont feel like writing the whole thing
      String output="";
      for(int count=0;count<s.length();count++)
      {
           output+=s.charAt(s.length()-1-count);
      //the += function works for string objects, but not other objects
      }
      This SHOULD work, but I havent bug tested the code I wrote above, you should be able to grasp the concept even if my implementation of it wasn't perfect.

      Again, this isn't the most convenient way of doing things, but you might be expected to write your own procedure for this.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by kotoro
        the method given by the previous answer is probably much more efficient than my answer is going to be, but if this is for a programming class, they probably don't want you to use built in sorts and toCharArray

        you could always create a new string and append (and save the resulting new string reference over the old string) the characters as you work backwards in the index of the input string.

        I'm assuming you're allowed to use loops.
        for loops are while loops with built in spaces for operations to be run at the start of the loop and at the end of each pass.

        to interpret my loop if you haven't been taught the use of "for" loops, you can convert it to a while loop.
        Code:
        For( (this spot executes at the start) ; (This is condition for continuing the loop) ; (this executes before checking the condition) )
        Code:
        //assume input of String s, I dont feel like writing the whole thing
        String output="";
        for(int count=0;count<s.length();count++)
        {
             output+=s.charAt(s.length()-1-count);
        //the += function works for string objects, but not other objects
        }
        This SHOULD work, but I havent bug tested the code I wrote above, you should be able to grasp the concept even if my implementation of it wasn't perfect.

        Again, this isn't the most convenient way of doing things, but you might be expected to write your own procedure for this.
        You function seems to me like it's just reversing the input string. The requirement was to sort the characters alphabetically.

        Comment

        • kotoro
          New Member
          • Nov 2006
          • 32

          #5
          I Must apologize, I only realized my error after the Edit period passed, my code reverses the string's character order.
          I didn't read closely enough, and Assumed. (insert cliche about assuming here)

          If you need to sort Characters the previous method is the way to go. You might be expected to write the sort algorithm yourself though. If you need to sort words, then you might want to write a loop that checks for spaces and cuts out words piece by piece.

          P.S. I feel like an ass now. I need to read more carefully before trying to help next time.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by kotoro
            I Must apologize, I only realized my error after the Edit period passed, my code reverses the string's character order.
            I didn't read closely enough, and Assumed. (insert cliche about assuming here)

            If you need to sort Characters the previous method is the way to go. You might be expected to write the sort algorithm yourself though. If you need to sort words, then you might want to write a loop that checks for spaces and cuts out words piece by piece.

            P.S. I feel like an ass now. I need to read more carefully before trying to help next time.
            Naturally I assumed the OP would write the sort function him/herself. After getting the logic it is the least I expected the OP to do.
            Yeah we've all overlooked something in a post before. The important thing is to be able to realise it (like you've done) and point it out for the help of others.

            Comment

            Working...