473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

remove non alphanumeric characters

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.

Mar 2 '07 #1
5 16820
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.

Mar 2 '07 #2
On 2 Mar, 16:51, "joe" <jcha...@gmail.comwrote:
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 ...)

Mar 2 '07 #3
"joe" <jc*****@gmail.comwrites:
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) ks***@mib.org <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"
Mar 2 '07 #4

"joe" <jc*****@gmail.comwrote 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

Mar 3 '07 #5
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.
On Mar 3, 3:33 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"joe" <jcha...@gmail.comwrote 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

Mar 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Tonks | last post by:
OK, here's possibly a weird one. Is there any way to do string comparisons and ignore all non-alphanumeric characters? For instance, check "foobar" and have it match an existing record of "f$#!oo...
1
by: Avnish | last post by:
Hi, I am looking for some form of validation for all the alphanumeric characters in the entire unicode range e.g. the validation should also accept japanese characters but should restrict...
4
by: Matt | last post by:
I want the javascript to test an alphanumeric (a string contains alphabet or numbers only) string. Should I write a regular expression? What's the best way to do? please help. thanks
10
by: Bob | last post by:
Sorting the following alphanumerics using myArray.sort(): 04-273-0001 04-272-0001 04-272-0003 04-272-0001 04-273-0001 Results in:
3
by: Bill | last post by:
I just ran into a situation where string data from a mainframe contained a couple of non-alphanumeric characters (hex CC and C8). I was parsing a field that occurred after these unexpected...
12
by: John | last post by:
Hi How can I select records that have non-alphanumeric characters in a field using a select query? Thanks Regards
7
by: kanepart2 | last post by:
Hey all, I have to validate a textbox in windows forms for alphanumeric characters such that non alphanumeric key presses are ignored. Some help would be appreciated
5
by: DotNetNewbie | last post by:
Hi, I want to parse a string, ONLY allowing alphanumeric characters and also the underscore '_' and dash '-' characters. Anything else in the string should be removed. I think my regex is...
3
by: DotNetNewbie | last post by:
Hi, I want to parse a string, ONLY allowing alphanumeric characters and also the underscore '_' and dash '-' characters. Anything else in the string should be removed. I think my regex is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.