Thread: String problem.

  1. #16
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    Acutally, I don't understand how it works since you approaches a memory which is not yours to access.
    Like people told you before size is not the element indicator because we start from 0.

  2. #17
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    I changed it completely I put a while loop in instead I think the code is ok now cheers.

  3. #18
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    sorry walla I don't understand what you mean.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Ok it's probably still wrong ha.. works fine though.



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

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why not use iterators? std::string provides forward and reverse iterators, accessible with begin/end and rbegin/rend. you could start a for loop, initializing two iterators with begin and rbegin, and increment both each time through the loop. as long as the values referred to by the iterators are the same, you continue the loop and add the current value of the iterator you received from begin to the end of your string.

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Ok it's probably still wrong ha.. works fine though.

    Works fine for which inputs? I liked your for loop better, honestly. The bug you had was really simple. Did you figure out what it was or did you just want to try the while loop? Note that your while loop actually has the same type of problem as your for loop did (you have to know when to stop).


    >> why not use iterators?

    For something this simple that is a learning exercise it's probably not necessary, especially since it's unlikely that caffrea4 has been introduced to them. Also note that he or she would have to solve the same problem for your pseudocode as in the for and while loops, so it wouldn't be any more correct than what is there already.

  8. #23
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    Quote Originally Posted by caffrea4 View Post
    sorry walla I don't understand what you mean.
    <=size wasn't good, it should be <size since the count begins from 0.

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