View Full Version : remove characters in c
alaios
07-10-2005, 04:20 PM
Hi i have the following file that includes the following data
3000Kb
2000b
1000KB
2000B
1500Mb
etc
as u can see the last two characters (ore one last character) is not a numeric... so i want to remove every non numeric character and produce the following
3000
2000
1000
2000
1500
How i can do it?
nikkiH
07-11-2005, 05:17 PM
Basically, find the index of the first character, then copy just the beginning into a new string.
(since classic C has no substring function that I could find)
Not tested, may be a typo or something...
int length=strlen(string)
for (int i=0; i < length; i++)
{
if (! isdigit(string[ i ]))
{ break; }
}
strncpy(string,newstr,i)
Notes:
1. you need string.h and ctype.h
2. I assume string and newstr are declared properly as character arrays and you do bounds checking
3. char *strncpy(const char *string1,const char *string2, size_t n) -- Copy first n characters of string2 to string1 .
4. You may need to take those spaces out of the array reference. The [ i ] thing was because this bboard thinks it means italics.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.