Posted by Fox on 2000-04-30
> Does anyone know how when I have matched a letter in a string to another
> letter in the same string using a nested for loop and and if statement
> (thanks Fox) , I can then remove the matched character.
>
> eg) eat my pretty pen
>
> becomes
>
> eat my pry n
>
> The problem for me is eliminating the repeated character and then moving the
> remaining chars back one place in the string to fill the gap of the removed
> character.
/*************************************/
I had to do something similiar to this, that's how I know these algorithims.
//METHOD ONE OF TWO: THE HARD WAY
As far as eliminating a character and bumping back everything else goes, it's
simple to do, but takes a little abstract thought.
To eliminate the part, remember where the occurrance is. Then, in a loop
>from that point, swap(switch places) that duplicate character with the next one in
line. Continue swapping that soon-to-be-dead character with one infront of it until
it gets to the end of the string. Then move it past the null terminator, which will(I
think and hope) place it outside the string and thus delete it:
//we're inside the case where you would do something if a duplicate character is
//found.
char tempchar;
//Here, place gets where the duplicate character is, and starts from there
for(place = subindex; place < strlen(string); place++)
{
//These lines swap the character you want to delete
//with the one infront of it, thus moving it forward withing the string.
tempchar = string[place];
string[place] = string[place + 1];
string[place + 1] = tempchar;
}
This simple loop moves the character forward within the string and, hopefully,
ends up moving it beyond the null terminator. I think that will end up deleting the
character, since all functions with strings stop at the null terminator. But the
character will still be there and the memory will still be there, so no memory will
change. Honestly, I'm not sure if there is a way to delete a character per se.
given the string, "that tough teacher", let's put it in the loop:
it finds the 't' at the end of "that" and starts the swapping. It first swaps it with the
space after "that" : "tha ttough teacher". Then with the 't' in "tough". Then it
swaps it with the 'o': "tha totugh teacher". And so on. Remember, you'll have to
do a test in each iteration of the loop for the end of string case, so it won't go
beyond the string boundry, but just swap with the null terminator.
//METHOD TWO: THE EASIER WAY, BUT MESSIER
I should say now, that if you REALLY want to, you could skip all the above, and
simply rebuild the string array to exclude that character. When the duplicate
character is found, create a new string variable the size of the origional string
minus one. THen loop through and set each element the same, but when you get
to the duplicate, skip it; don't write it into the new array! Then just continue on!
Once the new string is the same as the only one, minus the one character, set
the parameter pointer to the new string so the rest of the function will use the new
string instead of the old one.
//When the duplicate letter is found.
char *newstring[(strlen(string) - 1)];
for(i = 0; i < strlen(string); i++)
{
if(string[i] != string[skipover])
newstring[i] = string[i];
}
string = newstring; //I think this will set the string pointer to point to the new string.
//Now the rest of the program, accessing it through the "string"
//parameter pointer, will use the new string over the old one
The "skipover" variable is the place of the duplicate you want to delete. Just don't
copy it into the newstring.
There is a catch to this: the newstring can't just be a local variable. Once the
function returns, it goes out of scope and is destroyed. To keep the new string in
tact(to make changes to the newstring apply to the origional, too), you'll have to
either:
1) allocate it onto memory, and access it through pointers all the way through.
2) make sure the string in the origional is set to the new string.
3) set the string that you passed equal to the newstring, one character at a time(I
think that will work).
4) return the new string once finished, instead of changing it from the parameter.
Returning the new string with this method is probably the best and simplest
option. All this should work.
I'll get back to you on the 5x5 matrix later; if I have time. I hope this helps.
*******************************************
My brain is powered by C++
My hands are trained for the keyboard
My legs are built for the hockey floor
My ears are tuned to Mike Oldfield
My eyes are sensitive to video games
My attitude is sarcastic
My body is the work of God!
Previous post | Next post | Timeline | Home