Like Tree3Likes

String problem.

This is a discussion on String problem. within the C++ Programming forums, part of the General Programming Boards category; Acutally, I don't understand how it works since you approaches a memory which is not yours to access. Like people ...

  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
    21,390
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    2,288
    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,317
    >> 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.

Page 2 of 2 FirstFirst 12
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, 09:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21