remove non alphanumeric characters

J

joe

hello i have a databse program that uses char arrays to output data to
reports. I would like to remove all invalid characters from the array
and replace them with a blank space. I have problems with ( ' return
and some non ascii charcters. Any quick and dirty way to do this?
thanks.
 
S

santosh

joe said:
hello i have a databse program that uses char arrays to output data to
reports. I would like to remove all invalid characters from the array
and replace them with a blank space. I have problems with ( ' return
and some non ascii charcters. Any quick and dirty way to do this?
thanks.

Have a look at the various is* functions in ctype.h. A combination of
them should do what you want. For example ispunct returns true if the
argument is a non-alphanumeric non-space printable character.
Similarly iscntrl returns true if it's argument is a control
character. You can use such functions, (actually macros), to identify
and strip out the unwanted characters.

Beware some of the is* functions are specific to C99 which may not be
fully supported on most compilers.
 
M

mark_bluemel

hello i have a databse program that uses char arrays to output data to
reports. I would like to remove all invalid characters from the array
and replace them with a blank space. I have problems with ( ' return
and some non ascii charcters. Any quick and dirty way to do this?


Apart from scanning the array and checking each character with
isprint()?

I doubt it. But I don't think something like this (WARNING: untested)
is too hard:-

void cleanup(char *string) {
while(*string) {
if (!isprint(*string)) {
*string = ' ';
}
string++;
}
}

Adjust to your needs, but I think the isxxxx() functions in ctype.h
are what you need.

Variations are to declare a lookup table of valid characters and
validate against it, or (more efficiently) to do what I believe
isxxxx() normally does and setup an array of flags which can be
indexed by the character we are testing.

(I expect this will get torn to shreds ...)
 
K

Keith Thompson

joe said:
hello i have a databse program that uses char arrays to output data to
reports. I would like to remove all invalid characters from the array
and replace them with a blank space. I have problems with ( ' return
and some non ascii charcters. Any quick and dirty way to do this?

The first think you need to do is define what you mean by "invalid
characters".

You tell us you "have problems", but you don't tell us what the
problems are; that makes it impossible to suggest a solution.

Sometimes 90% of the effort of getting an answer is just asking the
right question.
 
M

Malcolm McLean

joe said:
hello i have a databse program that uses char arrays to output data to
reports. I would like to remove all invalid characters from the array
and replace them with a blank space. I have problems with ( ' return
and some non ascii charcters. Any quick and dirty way to do this?
thanks.
Why do it quick and dirty when a decent program only takes a minute?

/*
must a character be repalced by a space?
Params: ch - character to test
Returns: 1 if must be replaced, 0 if must be retained
*/
int replaceme(char ch)
{
if(isalnum(ch))
return 0;
if(isspace(ch))
{
if(ch == '\n' || ch == '\t')
return 0;
else
return 1;
}
/* other conditions here for punctuation and so on */
}

/*
This might need a substantial rewrite if you wish to distinguish gibberish
from a name which might have
one or two European or punctuation characters embedded in it, eg O'Rourke,
Bronte with two dots over the e, and so forth.
*/
void fixstring(char *str)
{
while(*str)
{
if(replaceme(*str))
*str = ' ';
str++;
}
}
 
J

joe

Thanks guys, I will try to test some of this code. I have two problems
that arise from weird character. I am storing the ouput from my sybase
database into a char array. Some times a weird character like ',
crashes the c program. Sometimes a '(' messes up the html pages. I am
sure there are more problems but those two, i remember. I will try
the is print trick. thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top