Reverse the string..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajujrk
    New Member
    • Aug 2008
    • 107

    Reverse the string..

    Reverse the string
    1) Use only one character variable
    2) Don't Use array
    3) Only one variable is allowed

    Pl.. any one help ...
  • questionit
    Contributor
    • Feb 2007
    • 553

    #2
    With a character variable , it is not possible to work with a string since the character variable can be assigned only single chracter.

    You should use 'string' type or an array to do this.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I wonder what sick mind comes up with these silly "do this and that, don't use
      so and so and only use blablabla" kind of riddles all the time. I don't like these
      imperatives in the text either; it must be homework.

      kind regards,

      Jos

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Perhaps a structure can help,or maybe pointer to a char(Hey,it's not literally an array).I've thougth something like this might help:

        Code:
        typedef struct _string
        {
           char _0;
           char _1;
           char _2;
           char _3:
           .
           .
           .
          //Is there a size limit?
          char _20;
        
        }Tstring;

        It is one variable,but of type Tstring.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          If you are not allowed even to use index variable or second pointer, I think it's impossible.

          If you can modify original pointer during execution, like
          reverse(char*s) {
          char c;
          ....

          , it's possible but takes o(n*n) ops, like the following: take first character, mark this position in string with zero, roll to end of string ( while (*s++){} ), exchange it with last-before-zero character, roll to the start of the string ( where zero is now), exchange, etc, until you encounter zero on the first step back.

          Comment

          • gulyan
            New Member
            • Aug 2008
            • 11

            #6
            do you mean you have a string, something like:
            Code:
            char str[] = "just a string";
            and you need to revers it with only one variable?
            if so you might try something like this:
            Code:
            Code removed per Posting Guidelines
            this way the only variable you use is "i" fot the indexing.
            this is a little absurd ... you have to calculate the length of str 5 times for every character. it would be simpler to have
            Code:
            n=strlen(str);
            on the begining and use n in the rest of the code.
            Last edited by sicarie; Aug 18 '08, 01:08 AM. Reason: Spoonfeeding code removed - OP has not contributed ANYTHING to this conversation aside of 1st post

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              You use recursion.

              On the call, put the character into the char. I the char is not 0, then, add 1 to the address of the char by coding &thechar+1. Use this as the arguiment on the recursive call.

              On a call, when char is 0, your string is in the call stack.

              Now return the char.

              Your string will be returned in reverse order.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by weaknessforcats
                You use recursion.

                On the call, put the character into the char. I the char is not 0, then, add 1 to the address of the char by coding &thechar+1. Use this as the arguiment on the recursive call.

                On a call, when char is 0, your string is in the call stack.

                Now return the char.

                Your string will be returned in reverse order.
                Can you show us the actual code because I don't understand your explanation.
                Of course I can put the reversed string somewhere else, e.g. on an output stream:

                [code=c]
                Code removed[/code]

                ... but how to put the chars back in to the memory pointed to by the initial value
                of char* p?

                kind regards,

                Jos
                Last edited by sicarie; Aug 18 '08, 01:08 AM. Reason: Spoonfeeding code removed - OP has not contributed ANYTHING to this conversation aside of 1st post

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Code:
                  Code removed
                  You really don't need to know where the string starts or ends. When you reach the end of the recursion at the last character, just push it into a stream.

                  Then in main(), just extract the stream into the original string.

                  The recursion uses only a char* and a stringstream& neither of which is a char or an array so it probably meets the problem requirement. You don't actually need a char variable.
                  Last edited by sicarie; Aug 18 '08, 01:07 AM. Reason: Spoonfeeding code removed - OP has not contributed ANYTHING to this conversation aside of 1st post

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by weaknessforcats
                    You really don't need to know where the string starts or ends. When you reach the end of the recursion at the last character, just push it into a stream.

                    Then in main(), just extract the stream into the original string.

                    The recursion uses only a char* and a stringstream& neither of which is a char or an array so it probably meets the problem requirement. You don't actually need a char variable.
                    Yup, that I understand but I understood differently from your previous post, i.e.
                    using no additional memory proportional to the size of the string, reverse it using
                    no other variables (except one according to the OP's rules). I considered it just
                    a little academic exercise ;-)

                    kind regards,

                    Jos

                    Comment

                    • whodgson
                      Contributor
                      • Jan 2007
                      • 542

                      #11
                      There is a standard C++ generic algorithm 'reverse()'
                      if char * s="ABCDE"
                      reverse(s,s+5)
                      cout<<s<<endl;//will print EDCBA

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by whodgson
                        There is a standard C++ generic algorithm 'reverse()'
                        if char * s="ABCDE"
                        reverse(s,s+5)
                        cout<<s<<endl;//will print EDCBA
                        You can't write to a string constant; it induces indefined behaviour so anything
                        can happen according to the Standard.

                        kind regards,

                        Jos

                        Comment

                        • whodgson
                          Contributor
                          • Jan 2007
                          • 542

                          #13
                          Quote:

                          You can't write to a string constant; it induces indefined behaviour so anything
                          can happen according to the Standard.

                          Could you amplify on the above as I am trying to learn Cpp.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            This is what the C99 Standard says about it:

                            Originally posted by C99
                            [#5] In translation phase 7, a byte or code of value zero is
                            appended to each multibyte character sequence that results
                            from a string literal or literals.56) The multibyte
                            character sequence is then used to initialize an array of
                            static storage duration and length just sufficient to
                            contain the sequence. For character string literals, the
                            array elements have type char, and are initialized with the
                            individual bytes of the multibyte character sequence; for
                            wide string literals, the array elements have type wchar_t,
                            and are initialized with the sequence of wide characters
                            corresponding to the multibyte character sequence, as
                            defined by the mbstowcs function with an implementation-
                            defined current locale. The value of a string literal
                            containing a multibyte character or escape sequence not
                            represented in the execution character set is
                            implementation-defined.

                            [#6] It is unspecified whether these arrays are distinct
                            provided their elements have the appropriate values. If the
                            program attempts to modify such an array, the behavior is
                            undefined.
                            Pay special attention to the last paragraph.

                            kind regards,

                            Jos

                            Comment

                            • SpecialKay
                              New Member
                              • Mar 2008
                              • 109

                              #15
                              I just have to say, the thread rules <content removed>. how is anyone suspost to learn when everything is removed?

                              Comment

                              Working...