Thread: Question

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    12

    Question

    Found this code on one of the topic. would u mind to explain what actually the code do

    Code:
    string Change_me(string my_string)
    {
        
        for(int i = 0; i <my_string.length(); i++)
        {
          if(isdigit(my_string[i])!=0)
          {
              if(isdigit(my_string[i+1])==0)
              {  
                  my_string.insert(i+1, "v");
                  //v is just an arbitary choice
                  //it could be any other letter
                  //but it has to be a LETTER
                     
              }    
          }    
        }
        //Changed -7*-7 case
        for ( i = 0; i <my_string.length(); i++)
        {
            if(my_string[i]=='-')
            {
                if((my_string[i-1]!='v')&&(my_string[i-1]!=')'))
                {
                   my_string.replace(i,1,"y"); 
                }
            }
        } 
     
    
        return my_string;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by isarapearl
    Found this code on one of the topic. would u mind to explain what actually the code do
    What does it do? Demonstrates an attempt to access memory outside the valid range of the string object that's what:
    Code:
    for(int i = 0; i <my_string.length(); i++)
    {
        if(isdigit(my_string[i ])!=0)
        {
            if(isdigit(my_string[i+1])==0)
            {  
                my_string.insert(i+1, "v");
                //v is just an arbitary choice
                //it could be any other letter
                //but it has to be a LETTER
            }    
        }    
    }
    What happens if the length of the string is 10 (valid indicies between 0 and 9 inclusive) and i is 9? The second call to isdigit attempts to access my_string[10]... which is bad.



    Code:
    for ( i = 0; i <my_string.length(); i++)
    {
        if(my_string[i ]=='-')
        {
            if((my_string[i-1]!='v')&&(my_string[i-1]!=')'))
            {
                my_string.replace(i,1,"y"); 
            }
        }
    }
    What happens when i is 0? Tell me what is my_string[-1] equal to?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM