Removing found chars from string
Hello all
I want to be able to search a string and remove non a-z chars and also search for certain words and then remove them
ie. char string[25]="STEPHEN JOHNSON - LTD"
So i use isalpha to get rid of the non a-z string
i am left with STEPHENJOHNSONLTD
LTD is the word i want to remove
How do i go about this?
Any help is much appreciated.
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
to get rid of non-alpha characters -- use a pointer to check each char using isalpha() macro. When non-alpha char found, call movemem() to shift all remaining characters lincluding the null terminator left one byte to cover up the non-alpha character.
After you get that working we cal talk about removing the word. Don't try to do all of it all at the same time or you will have trouble doing any of it.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I have managed to get my string from
"STEPHEN JOHNSON - LTD"
to
"STEPHENJOHNSONLTD"
No i need to remove LTD
Thanks
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
But this doesnt help me remove a word as such does it?
I need to remove the word LTD from string
STEPHENJOHNSONLTD
Thanks
sgriffiths
Junior Poster in Training
61 posts since Jun 2006
Reputation Points: 9
Solved Threads: 0
First form your string like "STEPHEN JOHNSON".
for (i=0; i<25; i++, string++)
{
if (*string == '-' && i != 0)
{
*(string-1) = '\0';
}
}
and then remove the space.
why does the loop count to 25? What if the string does not contain 25 characters, or if it contains more than 25 characters. Hard-coding a number there is not a good idea.
>> if (*string == '-' && i != 0)
syntax error.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
But this doesnt help me remove a word as such does it?
I need to remove the word LTD from string
STEPHENJOHNSONLTD
Thanks
use strstr() to find beginning of the word you want to remove, then use a pinter to shift everything left until either a space is found of end of string is found. Now lets see you post some code.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Hm read the first post
Yes, I misread it. That is a good example of why the programmer should use pleanty of white space..whats the posibility that the string have more than 25 chars?
100 % when the string contains some other characters. Loops like that should be made more general to handle strings of any length. Hard-coding the string length is just sloppy programming.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343