1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Access violation of initializing string

Discussion in 'Programming' started by michel, Aug 30, 2009.

  1. michel

    michel New Member

    Joined:
    Aug 30, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I tried to create a code that would delete any same words or phrases in a string that was passed by pointer parameter. This is the outcome:

    Code:
    [FONT=Lucida Console]void DeleteString(char *chString, char *chDelString)
     {
        int nDelStringLength = 0;
        while (chDelString[nDelStringLength] != 0)
            ++nDelStringLength;
        for (int nStringDelCount = 0; chString[nStringDelCount]
            != 0; ++nStringDelCount)
            if (chString[nStringDelCount] == chDelString[0])
                    {
                int nStringDelCount2 = nStringDelCount + 1;
                for (int nStringDelCount3 = 1; [/FONT][FONT=Lucida Console]hDelString[nStringDelCount3] != 0; ++nStringDelCount3)
                            {
                    if (chString[nStringDelCount2] != chDelString[nStringDelCount3])
                                            break;
                    if (chDelString[nStringDelCount3 + 1] == 0)
                                    {
                                            for (int nStringDelCount4 = 0; nStringDelCount4 <= nDelStringLength; ++nStringDelCount4)
                                            {
                                                    int nStringDelCount5;
                                                    nStringDelCount5 = nStringDelCount + 1;
                            if (nStringDelCount4 == 0)
                                                            nStringDelCount5 = nStringDelCount;
                                                    while (chString[nStringDelCount5] != 0)
                            {
                                                            if (nStringDelCount5 == nStringDelCount && chString[nStringDelCount5 - 1] == 32)
                                                                    for (int nStringDelCount6 = nStringDelCount - 1; chString[nStringDelCount6] != 0;
                                        ++nStringDelCount6)
                                    {
                                                                            chString[nStringDelCount6] = chString[nStringDelCount6 + 1];-------------
                                        if (chString[nStringDelCount6 + 1] == 0)
                                        {
                                                                                    --nStringDelCount5;
                                                                                    --nStringDelCount;
                                        }
                                                                     }
                                                                 chString[nStringDelCount5] = chString[nStringDelCount5 + 1];
                                                                 ++nStringDelCount5;
                                                                 --nStringDelCount;
                                                         }
                             }
                         }
                                         ++nStringDelCount2;
                     }
                 }
             }
    
    [/FONT]
    Consider the following statements:

    char *x = "a word and another word";
    DeleteString(x, "word");


    This is meant to remove any phrase that are labeled "word" plus any spaces before it. So when x is displayed on the screen, it should output "a and another". But, when it gets to the point in the function showed in ---... it tries to assign the space to the character next to it which in this case would be 'w' but the program breaks at that point and shows that it is an access violation. Does anyone know the solution to this problem?
     
    Last edited by a moderator: Aug 31, 2009
  2. michel

    michel New Member

    Joined:
    Aug 30, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am sorry about how the code was shown here is a better format:

    Code:
    void DeleteString(char *chString, char *chDelString)
    {
    	int nDelStringLength = 0;
    	while (chDelString[nDelStringLength] != 0)
    		++nDelStringLength;
    	for (int nStringDelCount = 0; chString[nStringDelCount] != 0; ++nStringDelCount)
    		if (chString[nStringDelCount] == chDelString[0])
    		{
    			int nStringDelCount2 = nStringDelCount + 1;
    			for (int nStringDelCount3 = 1; chDelString[nStringDelCount3] != 0; ++nStringDelCount3)
    			{
    				if (chString[nStringDelCount2] != chDelString[nStringDelCount3])
    					break;
    				if (chDelString[nStringDelCount3 + 1] == 0)
    				{
    					for (int nStringDelCount4 = 0; nStringDelCount4 <= nDelStringLength; ++nStringDelCount4)
    					{
    						int nStringDelCount5;
    						nStringDelCount5 = nStringDelCount + 1;
    						if (nStringDelCount4 == 0)
    							nStringDelCount5 = nStringDelCount;
    						while (chString[nStringDelCount5] != 0)
    						{
    							if (nStringDelCount5 == nStringDelCount && chString[nStringDelCount5 - 1] == 32)
    								for (int nStringDelCount6 = nStringDelCount - 1; chString[nStringDelCount6] != 0;
    									++nStringDelCount6)
    								{
    									chString[nStringDelCount6] = chString[nStringDelCount6 + 1];
    									if (chString[nStringDelCount6 + 1] == 0)
    									{
    										--nStringDelCount5;
    										--nStringDelCount;
    									}
    								}
    							chString[nStringDelCount5] = chString[nStringDelCount5 + 1];
    							++nStringDelCount5;
    							--nStringDelCount;
    						}
    					}
    				}
    				++nStringDelCount2;
    			}
    		}
    }
    
     

Share This Page