String Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suyash Upadhyay
    New Member
    • Mar 2007
    • 34

    String Problem

    Hello all,
    this question was asked in an interview..
    you have a string xCy consists only 'A' and 'B' , where x,C and y all are string but y is in reverse order of x,i.e., if x is 'ABAAB' then y should be 'BAABA'.
    I was to find out whether a particular string is of the form of xCy or not. I could only access next character of string. ( so it was not possible to compare first element to last one, then second to second last and so on)

    Thanx and Regards,
    Suyash
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I'm not clear as to your question.

    I understand that a string contains on 'A' and 'B' characters.

    However, xCy cannot be a string if x is a string. Do you mean an array of 3 strings??

    And, is this a C++ answer you want or a C answer?

    Comment

    • Suyash Upadhyay
      New Member
      • Mar 2007
      • 34

      #3
      Originally posted by weaknessforcats
      I'm not clear as to your question.

      I understand that a string contains on 'A' and 'B' characters.

      However, xCy cannot be a string if x is a string. Do you mean an array of 3 strings??

      And, is this a C++ answer you want or a C answer?
      Yes, I mean array of 3 strings.

      Answer in C or C++ both are OK with me, because I am confident in both languages.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Here is a compare function using string objects that returns true if the first string is 1234 and the second string is 4321 and false otherwise. That is, the strings are identical but reversed.
        [code=cpp]
        bool Compare(const string& first, const string& second)
        {
        string::const_r everse_iterator ritr = second.rbegin() ;
        string::const_i terator itr = first.begin();
        while ((itr != first.end()) && (ritr != second.rend()))
        {
        if (*itr != *ritr) return false;
        ++itr;
        ++ritr;
        }
        return true;
        }
        [/code]

        Just create you array of strings and call this function with two of the array members.

        The function uses forward and reverse iterators instead of naked pointers.

        Comment

        • Girish Kanakagiri
          New Member
          • May 2007
          • 93

          #5
          Originally posted by weaknessforcats
          Here is a compare function using string objects that returns true if the first string is 1234 and the second string is 4321 and false otherwise. That is, the strings are identical but reversed.
          [code=cpp]
          bool Compare(const string& first, const string& second)
          {
          string::const_r everse_iterator ritr = second.rbegin() ;
          string::const_i terator itr = first.begin();
          while ((itr != first.end()) && (ritr != second.rend()))
          {
          if (*itr != *ritr) return false;
          ++itr;
          ++ritr;
          }
          return true;
          }
          [/code]

          Just create you array of strings and call this function with two of the array members.

          The function uses forward and reverse iterators instead of naked pointers.
          I think the above code cannot be the answer, because if one of the two string
          reaches the end and the other has still not reached the end; then while loop ends and returns TRUE but actually it is not TRUE.

          Regards,
          Girish.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Girish Kanakagiri
            I think the above code cannot be the answer, because if one of the two string
            reaches the end and the other has still not reached the end; then while loop ends and returns TRUE but actually it is not TRUE.

            Regards,
            Girish.
            Yep, I think the strings should be checked for equal length before the comparison begins.

            Comment

            • Girish Kanakagiri
              New Member
              • May 2007
              • 93

              #7
              Originally posted by r035198x
              Yep, I think the strings should be checked for equal length before the comparison begins.
              That's right. !!!

              Regards,
              Girish.

              Comment

              Working...