Thread: String problem.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    String problem.

    can anyone help me with a problem I'm kinda new to C++. Here is my question:

    Write a function of the form string mirrorEnds(string), that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very beginning of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab".

    mirrorEnds("abXYZba") → "ab"
    mirrorEnds("abca") → "a"
    mirrorEnds("navan") → "navan"

    I think I can do it if I can convert the string into an array of chars can this be done?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >I can convert the string into an array of chars can this be done
    Well... std::strings are internally array of characters.. and you can still get individual chars using the [] operator...just like you'd do in a normal array.
    Try doing something like the following:
    Code:
    Iterate over the string....(Let i be the index of the current position)
        if the character in the i'th position == char at (size - i)'th position
            append the the i'th char on the result string
        else break out from the loop

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    that's great that helps a lot thanks very much.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    ok here's my code I'm getting an error with the strcat function

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    string mirrorends(string s)
    { 
           int size = s.size();
           string mirror = " ";
           
    
           for (int i = 0; i <=size; i++)
           {
               if (s[i]==s[size -i])
               {
                   strcat(mirror,s[i]);
               }
           }
           
           cout << mirror << endl;
           return 0;
    }
    
    
    
    int main(int argc, char *argv[])
    {
        mirrorends("racecar");
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    That is because you're using a cstring function for C++ strings.
    Just use the + operator to append.
    Also..you seem to misunderstand what returning values from functions entail....Read up on that topic too.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> #include <string.h>
    That should be #include <string>. The one with the .h is for C style string functions like strcat, but you don't need or want those functions here since the instructions say to use the C++ string class in <string> and that class has everything you need.

    >> string mirror = " ";
    string mirror; is fine since it defaults to empty. Your version adds a space character, which will make the result wrong.

    Otherwise understand and implement manasij7479's advice and you should be good.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    ok changed my code again and it compiles and runs but displays nothing.

    here's my new (and hopefully improved code):

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string mirrorends(string s)
    { 
           int size = s.size();
           string mirror;
           
    
           for (int i = 0; i <=size; i++)
           {
               if (s[i]==s[size -i])
               {
                 mirror = mirror + s[i];
               }
           }
           
           return mirror;
    }
    
    
    
    int main(int argc, char *argv[])
    {
        string a = mirrorends("racecar");
        cout << a << endl;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Now you just have to debug your program. This is a great example of how to debug since you know the word you're inputting into the function and so you can follow the flow yourself to see what is happening. If you don't have or don't know how to use a debugger, just do it by hand.

    To start, you call mirrorends with the parameter "racecar", so inside that function the variable s is "racecar". So go to the first line. What is size? Write that down. Then the next line defines mirror as an empty string, so write down mirror and its current value.

    Then move on to the for loop. First write down the new variable i and its initial value. Then check to see if i is less than or equal to size. Then enter the loop. Now, what character is at s[i]? Also, what is size-i and what character is at s[size-i]?

    I'll leave you with that for now. Follow that type of logic and see if you get the results you expect. You should quickly see where the problem is, and then you can think about how to solve it.

    By the way, make sure to remember that C++ strings are different than C style strings if you're used to those. There is no null-terminator or anything like that. The string is just the characters you see.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    yeah I followed all that but still can't find my problem I know it's in the for loop when I try append the characters.
    so s[i] is my first character of the string and I want to compare it to the last character, [size-i].
    so when loop starts i =o, so s[i] gives me s[0] which is the first character,
    then size = 7, so i get the last character s[7-0].

    I fear this problem is so simple and just stearing me in the face ha. What am I doing wrong?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Write down the value of the string at those positions.
    Don't just assume. If you note you want to access s[7], then count 8 characters from the beginning and write down what that character is.
    Remember that indexing starts at 0, not 1!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    | r | a | c | e | c | a | r |
    | 0 | 1 | 2 | 3 | 4 | 5 | 6 |

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    oh just sank in there ha thnk you Elysia, Daved and Manasij7479. Code now works perfect

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    1
    Can you shed some light on how your final code works?

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    when I had s[size - i] which is [7-0] I was comparing my first character to the character at s[7] which is not the last character as Daved and Elysia pointed out. So my first character did not = to the character at s[7] so the code never entered the loop and just displayed the blank string.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
     
    using namespace std;
     
    string mirrorends(string s)
    {
           int size = s.size();
           string mirror;
            
     
           for (int i = 0; i <=size; i++)
           {
               if (s[i]==s[size -(i+1)])
               {
                 mirror = mirror + s[i];
               }
           }
            
           return mirror;
    }
     
     
     
    int main(int argc, char *argv[])
    {
        string a = mirrorends("racecar");
        cout << a << endl;
         
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    By the way... there is still a bug in your code.

    Continue walking through the loop to find it. Also make sure to test other words besides racecar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM