473,587 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16842
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(*stri ng)) {
*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_Keit h) 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(*s tr))
*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...@btin ternet.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(*s tr))
*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
3632
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 B(a)*R". - Daniel
1
10403
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 japanese punctuation marks. I need this validation for atleast for the CJKT range if not possible for the entire unicode range. I can even make use of...
4
11161
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
3641
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
2447
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 characters and it appears the Substring method was thrown off and returned a field two bytes off. Does this data cause a problem with the Substring method?
12
17126
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
21427
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
10383
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 looking like: ^()*$
3
20585
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 looking like: ^()*$
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.