Reverse a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sinjanv
    New Member
    • May 2010
    • 6

    Reverse a string

    How to write a c program tat takes the string as input and convert it into bits and reverse the string
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Treat the string as an array. Get the size (length)

    Should be possible in one loop incrementing upto size,
    and within the loop decrement a count down from size.

    With both these values copy array into new string array.
    Roughly
    Code:
    back = size;
    for(c=0;c<size;c++)
    {
        newStr[c] = strArray[size];
        back --;
    }

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Small correction

      Code:
      newStr[c] = strArray[back];
      back--;
      Regards
      Dheeraj Joshi

      Comment

      • Sinjanv
        New Member
        • May 2010
        • 6

        #4
        Originally posted by Sinjanv
        How to write a c program tat takes the string as input and convert it into bits and reverse the string
        but how to convert the string into bits first

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          bits and not bytes?
          You can do what they're doing for the first part, but then reverse each character's bits.
          pseudo:
          Code:
          get value c
          set newValue = 0
          shifts = size of c in bits. 
          while (shifts > 0){
            if c & 1  == 1
              newValue++;
            c >>= 1;
            if (shifts > 1)
              newValue <<= 1;
            shifts --;
          }
          then use newValue.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            What exactly do you mean by "convert the string to bits"? Please provide an example of an input string and the output you want.

            Comment

            • Sinjanv
              New Member
              • May 2010
              • 6

              #7
              Originally posted by donbock
              What exactly do you mean by "convert the string to bits"? Please provide an example of an input string and the output you want.
              example an input string consists of sinjanv and the last letter v in the string is a character this needs to be converted into bits and reverse the bits such tat it output give a new character

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                OK, the input string is "sinjanv". You called it a string, so it has to be null-terminated. What is the expected output string? Please be specific, otherwise I'm forced to guess what you mean by "reverse the bits".

                Comment

                • whodgson
                  Contributor
                  • Jan 2007
                  • 542

                  #9
                  The way I understand it this function prints the string in reverse order by accessing each character in memory in the reverse order of its assignment.
                  Code:
                  void reverse ()
                  { char c;
                  cin.get(c);
                  if(c=='\n')return;
                  reverse();
                  cout<<c;
                  return;
                  }

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    If the OP wants to reverse the chars in a string then:
                    "abcd" becomes "dcba"

                    If the OP wants to reverse the bits in a string then ... it depends what he means by that.
                    "abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} for ASCII encoding.
                    does "abcd" become {0x86, 0x46, 0xC6, 0x26, 0x00}
                    or does "abcd" become {0x26, 0xC6, 0x46, 0x86, 0x00}
                    or does "abcd" become {0x00, 0x26, 0xC6, 0x46, 0x86}

                    I hesitate to offer detailed advice until I'm sure what the OP wants to do.

                    Comment

                    • whodgson
                      Contributor
                      • Jan 2007
                      • 542

                      #11
                      Yes...see what you mean.

                      Comment

                      • Sinjanv
                        New Member
                        • May 2010
                        • 6

                        #12
                        Originally posted by donbock
                        If the OP wants to reverse the chars in a string then:
                        "abcd" becomes "dcba"

                        If the OP wants to reverse the bits in a string then ... it depends what he means by that.
                        "abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} for ASCII encoding.
                        does "abcd" become {0x86, 0x46, 0xC6, 0x26, 0x00}
                        or does "abcd" become {0x26, 0xC6, 0x46, 0x86, 0x00}
                        or does "abcd" become {0x00, 0x26, 0xC6, 0x46, 0x86}

                        I hesitate to offer detailed advice until I'm sure what the OP wants to do.
                        THE abcd becomes {0x61, 0x62, 0x63, 0x64, 0x00} since the given is in hexadecimal form i need to convert into binary in C one character= 8bits so the 8bits are reversed so that it becomes a new character

                        Comment

                        • donbock
                          Recognized Expert Top Contributor
                          • Mar 2008
                          • 2427

                          #13
                          Originally posted by Sinjanv
                          THE abcd becomes {0x61, 0x62, 0x63, 0x64, 0x00} since the given is in hexadecimal form i need to convert into binary in C one character= 8bits so the 8bits are reversed so that it becomes a new character
                          Please stop making me guess.
                          If "abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} then what is the expected output after that string goes into your program?

                          Comment

                          Working...