| | |
Removing found chars from string
![]() |
•
•
Join Date: Jun 2006
Posts: 61
Reputation:
Solved Threads: 0
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.
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.
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.
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.
First form your string like "STEPHEN JOHNSON".
and then remove the space.
c Syntax (Toggle Plain Text)
for (i=0; i<25; i++, string++) { if (*string == '-' && i != 0) { *(string-1) = '\0'; } }
If you want to win, you must not loose (Alan Ford)
•
•
•
•
First form your string like "STEPHEN JOHNSON".
and then remove the space.c Syntax (Toggle Plain Text)
for (i=0; i<25; i++, string++) { if (*string == '-' && i != 0) { *(string-1) = '\0'; } }
>> 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.
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.
•
•
•
•
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.
•
•
•
•
Originally Posted by sgriffiths
ie. char string[25]="STEPHEN JOHNSON - LTD"
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { char p[25] = "STEPHEN JOHNSON - LTD"; char *string = p; unsigned char i; printf("%s\n", p); for (i=0; i<25; i++, string++) { if (*string == '-' && i != 0) { *(string-1) = '\0'; break; /* added this line */ } } printf("%s\n", p); return 0; }
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)
Yes, I misread it. That is a good example of why the programmer should use pleanty of white space..
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.
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.
![]() |
Other Threads in the C Forum
- Previous Thread: Text color and columnizing help
- Next Thread: Removing characters from a string
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






