Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > remove non alphanumeric characters

Reply
Thread Tools

remove non alphanumeric characters

 
 
joe
Guest
Posts: n/a
 
      03-02-2007
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.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-02-2007
joe wrote:
> 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.

 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      03-02-2007
On 2 Mar, 16:51, "joe" <jcha...@gmail.com> wrote:
> 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 ...)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-02-2007
"joe" <> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      03-03-2007

"joe" <> wrote in message
> 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++;
}
}

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
joe
Guest
Posts: n/a
 
      03-05-2007
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.
On Mar 3, 3:33 am, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "joe" <jcha...@gmail.com> wrote in message
> > 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++;
> }
>
> }
>
> --
> Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.match and non-alphanumeric characters The Web President Python 8 11-17-2008 12:02 AM
Password length minimum: 7. Non-alphanumeric characters required. Yasin Cepeci ASP .Net Security 2 04-26-2007 10:11 PM
Password length minimum: 7. Non-alphanumeric characters required: 1. Yasin Cepeci ASP .Net 1 04-26-2007 05:09 PM
Newbie Question: delete all non alphanumeric characters Theallnighter Theallnighter Ruby 15 07-22-2006 08:52 PM
Easy way to specify all non-alphanumeric characters? Steven J Sobol Java 8 04-30-2004 09:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57