944,134 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4655
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 7th, 2006
0

Removing found chars from string

Expand Post »
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.
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Nov 7th, 2006
0

Re: Removing found chars from string

If other strings are similar remove everything after the second space.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 7th, 2006
0

Re: Removing found chars from string

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Nov 7th, 2006
0

Re: Removing found chars from string

I have managed to get my string from

"STEPHEN JOHNSON - LTD"

to

"STEPHENJOHNSONLTD"

No i need to remove LTD

Thanks
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Nov 7th, 2006
0

Re: Removing found chars from string

First form your string like "STEPHEN JOHNSON".
  1. for (i=0; i<25; i++, string++)
  2. {
  3. if (*string == '-' && i != 0)
  4. {
  5. *(string-1) = '\0';
  6.  
  7. }
  8. }
and then remove the space.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 7th, 2006
0

Re: Removing found chars from string

But this doesnt help me remove a word as such does it?

I need to remove the word LTD from string

STEPHENJOHNSONLTD

Thanks
Reputation Points: 9
Solved Threads: 0
Junior Poster in Training
sgriffiths is offline Offline
61 posts
since Jun 2006
Nov 7th, 2006
0

Re: Removing found chars from string

Click to Expand / Collapse  Quote originally posted by andor ...
First form your string like "STEPHEN JOHNSON".
  1. for (i=0; i<25; i++, string++)
  2. {
  3. if (*string == '-' && i != 0)
  4. {
  5. *(string-1) = '\0';
  6.  
  7. }
  8. }
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Nov 7th, 2006
0

Re: Removing found chars from string

Click to Expand / Collapse  Quote originally posted by sgriffiths ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
Nov 7th, 2006
0

Re: Removing found chars from string

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.
Hm read the first post
Quote originally posted by sgriffiths ...
ie. char string[25]="STEPHEN JOHNSON - LTD"
Whats the posibility that the string have more than 25 chars?
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. char p[25] = "STEPHEN JOHNSON - LTD";
  6. char *string = p;
  7. unsigned char i;
  8. printf("%s\n", p);
  9.  
  10. for (i=0; i<25; i++, string++)
  11. {
  12. if (*string == '-' && i != 0)
  13. {
  14. *(string-1) = '\0';
  15. break; /* added this line */
  16. }
  17. }
  18. printf("%s\n", p);
  19. return 0;
  20. }
Well I didn't posted the whole code.
Only thing is that I should break when '-' found.
EDIT: Simpler than using strlen
Last edited by andor; Nov 7th, 2006 at 9:05 am.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 7th, 2006
0

Re: Removing found chars from string

Click to Expand / Collapse  Quote originally posted by andor ...
Hm read the first post
Yes, I misread it. That is a good example of why the programmer should use pleanty of white space..

Click to Expand / Collapse  Quote originally posted by andor ...
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.
Last edited by Ancient Dragon; Nov 7th, 2006 at 9:12 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Text color and columnizing help
Next Thread in C Forum Timeline: Removing characters from a string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC