Removing found chars from string

Reply

Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Removing found chars from string

 
0
  #1
Nov 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Removing found chars from string

 
0
  #2
Nov 7th, 2006
If other strings are similar remove everything after the second space.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Removing found chars from string

 
0
  #3
Nov 7th, 2006
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Removing found chars from string

 
0
  #4
Nov 7th, 2006
I have managed to get my string from

"STEPHEN JOHNSON - LTD"

to

"STEPHENJOHNSONLTD"

No i need to remove LTD

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Removing found chars from string

 
0
  #5
Nov 7th, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 61
Reputation: sgriffiths is an unknown quantity at this point 
Solved Threads: 0
sgriffiths sgriffiths is offline Offline
Junior Poster in Training

Re: Removing found chars from string

 
0
  #6
Nov 7th, 2006
But this doesnt help me remove a word as such does it?

I need to remove the word LTD from string

STEPHENJOHNSONLTD

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Removing found chars from string

 
0
  #7
Nov 7th, 2006
Originally Posted by andor View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Removing found chars from string

 
0
  #8
Nov 7th, 2006
Originally Posted by sgriffiths View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Removing found chars from string

 
0
  #9
Nov 7th, 2006
Originally Posted by Ancient Dragon View Post
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
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Removing found chars from string

 
0
  #10
Nov 7th, 2006
Originally Posted by andor View Post
Hm read the first post
Yes, I misread it. That is a good example of why the programmer should use pleanty of white space..

Originally Posted by andor View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC