check this out. compile it and run it and see how it works.
the function "stripCharacters" will strip out any undesired characters using the standard library function
... very powerful function, learn to use it.
#include <stdio.h>
#include <string.h>
#define MAXLEN 32
//////////////////////////////////////////////////////
//
// function : stripCharacters
//
// input : character string to be modified
// output : the modified string (original is lost!!)
// return : number of chars stripped, or -1 if error
//
//////////////////////////////////////////////////////
int stripCharacters(char * user_str)
{
char copy_str[MAXLEN], *ptr;
const char CHARS_TO_STRIP[10] = ",$%#";
//error check, make sure string is not too long
int stringlen = strlen(user_str);
if (stringlen > MAXLEN)
return -1;
//make copy of user string, modified version will replace it
strncpy (copy_str,user_str,MAXLEN);
//look for first instance of "strip characters"
ptr = strtok(copy_str, CHARS_TO_STRIP);
strcpy(user_str,ptr);
//look for additional strip characters
while(ptr)
{
ptr = strtok(NULL, CHARS_TO_STRIP);
if (ptr)
strcat(user_str,ptr);
}
//modified string is passed back in original string variable
//return value indicates number of chars stripped, if any
return (stringlen - strlen(user_str));
}
int main()
{
char myString[32];
int retVal;
strcpy(myString,"1,234,567,890,123");
printf("original: %s\n",myString);
retVal = stripCharacters(myString);
printf("modified: %s (stripped %i)\n\n",myString,retVal);
strcpy(myString,"$5,299.99");
printf("original: %s\n",myString);
retVal = stripCharacters(myString);
printf("modified: %s (stripped %i)\n\n",myString,retVal);
strcpy(myString,"0.039%");
printf("original: %s\n",myString);
retVal = stripCharacters(myString);
printf("modified: %s (stripped %i)\n\n",myString,retVal);
strcpy(myString,"#103");
printf("original: %s\n",myString);
retVal = stripCharacters(myString);
printf("modified: %s (stripped %i)\n\n",myString,retVal);
strcpy(myString,"999888777");
printf("original: %s\n",myString);
retVal = stripCharacters(myString);
printf("modified: %s (stripped %i)\n\n",myString,retVal);
strcpy(myString,"123,456,789,012,345,678,901,234,567,890");
printf("original: %s\n",myString);
retVal = stripCharacters(myString);
printf("modified: %s (stripped %i)\n\n",myString,retVal);
return 0;
}
.