All of lore.kernel.org
 help / color / mirror / Atom feed
* comparing char to other known char's
@ 2005-06-22 23:22 James Colannino
  2005-06-22 23:44 ` David L. Martin
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: James Colannino @ 2005-06-22 23:22 UTC (permalink / raw)
  To: linux-c-programming

Hey everyone.  I hope this isn't a stupid question.  I've been googling
around trying to find a function that I can use but haven't been
successful.  Here's what I want to be able to do:

let's say I have a char called 'character.'  I want to compare
'character' to see if it's any one of the characters in a list.  For
example, maybe I would want to test character to see if it's either 'e',
'r', '*', etc.

Is this easy enough to implement?  I could do if (character == 'e' ||
character == [...] and so on and so forth, but this seems much to
tedious and unreadable to be my only solution.  If anyone has any ideas
I'd be extremely grateful :)  Thanks very much in advance.

James

-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"A well regulated militia being necessary to the security of a free
state, THE RIGHT of the people to keep and bear arms SHALL NOT BE
INFRINGED." --United States Constitution, Second Ammendment


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-22 23:22 comparing char to other known char's James Colannino
@ 2005-06-22 23:44 ` David L. Martin
  2005-06-22 23:46 ` Eric Bambach
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: David L. Martin @ 2005-06-22 23:44 UTC (permalink / raw)
  To: James Colannino; +Cc: linux-c-programming

On Wed, 2005-06-22 at 16:22 -0700, James Colannino wrote:
> let's say I have a char called 'character.'  I want to compare
> 'character' to see if it's any one of the characters in a list.  For
> example, maybe I would want to test character to see if it's either 'e',
> 'r', '*', etc.

if(strchr( teststr, character) != NULL)
	found();
else
	not_found();



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-22 23:22 comparing char to other known char's James Colannino
  2005-06-22 23:44 ` David L. Martin
@ 2005-06-22 23:46 ` Eric Bambach
  2005-06-23  0:25 ` James Colannino
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Eric Bambach @ 2005-06-22 23:46 UTC (permalink / raw)
  To: James Colannino

On Wednesday 22 June 2005 06:22 pm, James Colannino wrote:
> Hey everyone.  I hope this isn't a stupid question.  I've been googling
> around trying to find a function that I can use but haven't been
> successful.  Here's what I want to be able to do:
>
> let's say I have a char called 'character.'  I want to compare
> 'character' to see if it's any one of the characters in a list.  For
> example, maybe I would want to test character to see if it's either 'e',
> 'r', '*', etc.
>
> Is this easy enough to implement?  I could do if (character == 'e' ||
> character == [...] and so on and so forth, but this seems much to
> tedious and unreadable to be my only solution.  If anyone has any ideas
> I'd be extremely grateful :)  Thanks very much in advance.
>
> James

Fill an array with the ascii table

char array[256];

for( int i = 0;i < 256;i++){
 array[i] = i;
}

then mask out the characters you dont want matched.
 array[69] = 0;
 array[72] = 0;

Then use the character you receive as the index.
if ( array[character ){
 process this char...
}

There are a few other tricks too....perhaps this will work for you?
-- 
----------------------------------------
--EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

                --Alan Cox LKML-December 08,2000 

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-22 23:22 comparing char to other known char's James Colannino
  2005-06-22 23:44 ` David L. Martin
  2005-06-22 23:46 ` Eric Bambach
@ 2005-06-23  0:25 ` James Colannino
  2005-06-23 13:10 ` Adrian Popescu
  2005-06-25 11:58 ` HIToC
  4 siblings, 0 replies; 14+ messages in thread
From: James Colannino @ 2005-06-23  0:25 UTC (permalink / raw)
  To: linux-c-programming

Thanks for the suggestions.  They were helpful :)

James
-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

" Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety." --Benjamin Franklin

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-22 23:22 comparing char to other known char's James Colannino
                   ` (2 preceding siblings ...)
  2005-06-23  0:25 ` James Colannino
@ 2005-06-23 13:10 ` Adrian Popescu
  2005-06-23 20:40   ` James Colannino
  2005-06-25 11:58 ` HIToC
  4 siblings, 1 reply; 14+ messages in thread
From: Adrian Popescu @ 2005-06-23 13:10 UTC (permalink / raw)
  To: James Colannino; +Cc: linux-c-programming

On Thursday 23 June 2005 02:22, James Colannino wrote:
> Hey everyone.  I hope this isn't a stupid question.  I've been googling
> around trying to find a function that I can use but haven't been
> successful.  Here's what I want to be able to do:
>
> let's say I have a char called 'character.'  I want to compare
> 'character' to see if it's any one of the characters in a list.  For
> example, maybe I would want to test character to see if it's either 'e',
> 'r', '*', etc.
>
> Is this easy enough to implement?  I could do if (character == 'e' ||
> character == [...] and so on and so forth, but this seems much to
> tedious and unreadable to be my only solution.  If anyone has any ideas
> I'd be extremely grateful :)  Thanks very much in advance.
>
> James

the way you put it ( if (character == 'e' || ....) ; it is TRUE if 
any (or only one) from those chars exists ; 
perhaps you should be more specific


here is a starting point: 

#include <stdio.h>

#define haystack "abcdefgr*dsfsdfdfs"
#define needle "rte*"

int find_char(char *str , char ch) {
int i=0;
 while (str[i] != '\0') {
   if (ch==str[i]) return ch;
   i++;
 }
return 0;
}

int main (){
char *cp;
int i;
int n=0;

cp=needle;

while (cp[n] !='\0') {
 i=find_char (haystack, cp[n]);
 if(i) printf ("found dec: %d char: %c\n", i, i);
 n++;
}

}


--
Adrian

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-23 13:10 ` Adrian Popescu
@ 2005-06-23 20:40   ` James Colannino
  2005-06-23 22:57     ` Eric Bambach
  0 siblings, 1 reply; 14+ messages in thread
From: James Colannino @ 2005-06-23 20:40 UTC (permalink / raw)
  To: linux-c-programming

Adrian Popescu wrote:
> On Thursday 23 June 2005 02:22, James Colannino wrote:
> 
>>Hey everyone.  I hope this isn't a stupid question.  I've been googling
>>around trying to find a function that I can use but haven't been
>>successful.  Here's what I want to be able to do:
>>
>>let's say I have a char called 'character.'  I want to compare
>>'character' to see if it's any one of the characters in a list.  For
>>example, maybe I would want to test character to see if it's either 'e',
>>'r', '*', etc.
>>
>>Is this easy enough to implement?  I could do if (character == 'e' ||
>>character == [...] and so on and so forth, but this seems much to
>>tedious and unreadable to be my only solution.  If anyone has any ideas
>>I'd be extremely grateful :)  Thanks very much in advance.
>>
>>James
> 
> 
> the way you put it ( if (character == 'e' || ....) ; it is TRUE if 
> any (or only one) from those chars exists ; 

I know.  That's what I wanted.  Basically, I want to check to see if
each character in a string matches any one of an illegal set of
characters.  Thus, the function would fail if it encountered an illegal
character in the string.

James
-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

" Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety." --Benjamin Franklin

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-23 20:40   ` James Colannino
@ 2005-06-23 22:57     ` Eric Bambach
  2005-06-23 23:58       ` James Colannino
  2005-06-24  0:25       ` James Colannino
  0 siblings, 2 replies; 14+ messages in thread
From: Eric Bambach @ 2005-06-23 22:57 UTC (permalink / raw)
  To: James Colannino; +Cc: linux-c-programming

On Thursday 23 June 2005 03:40 pm, James Colannino wrote:
> Adrian Popescu wrote:
> > On Thursday 23 June 2005 02:22, James Colannino wrote:
> >>Hey everyone.  I hope this isn't a stupid question.  I've been googling
> >>around trying to find a function that I can use but haven't been
> >>successful.  Here's what I want to be able to do:
> >>
> >>let's say I have a char called 'character.'  I want to compare
> >>'character' to see if it's any one of the characters in a list.  For
> >>example, maybe I would want to test character to see if it's either 'e',
> >>'r', '*', etc.
> >>
> >>Is this easy enough to implement?  I could do if (character == 'e' ||
> >>character == [...] and so on and so forth, but this seems much to
> >>tedious and unreadable to be my only solution.  If anyone has any ideas
> >>I'd be extremely grateful :)  Thanks very much in advance.
> >>
> >>James
> >
> > the way you put it ( if (character == 'e' || ....) ; it is TRUE if
> > any (or only one) from those chars exists ;
>
> I know.  That's what I wanted.  Basically, I want to check to see if
> each character in a string matches any one of an illegal set of
> characters.  Thus, the function would fail if it encountered an illegal
> character in the string.
>
> James

Generally speaking (in terms of input validation), its better practice to 
check against a LEGAL set of characters rather than an illegal set. That way 
you can get all the characters you need, but everything else is blocked. If 
you block illegal ones you're bound to miss a few or even ones from extended 
charsets and input methods that you might not have thought of that could 
wreck havoc in your program.

HTH!

-- 
----------------------------------------
--EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

                --Alan Cox LKML-December 08,2000 

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-23 22:57     ` Eric Bambach
@ 2005-06-23 23:58       ` James Colannino
  2005-06-24  0:25       ` James Colannino
  1 sibling, 0 replies; 14+ messages in thread
From: James Colannino @ 2005-06-23 23:58 UTC (permalink / raw)
  To: linux-c-programming

Eric Bambach wrote:

> Generally speaking (in terms of input validation), its better practice to 
> check against a LEGAL set of characters rather than an illegal set. That way 
> you can get all the characters you need, but everything else is blocked. If 
> you block illegal ones you're bound to miss a few or even ones from extended 
> charsets and input methods that you might not have thought of that could 
> wreck havoc in your program.

You're right.  I didn't think of that.  Thanks for the warning.

James
-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

" Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety." --Benjamin Franklin

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-23 22:57     ` Eric Bambach
  2005-06-23 23:58       ` James Colannino
@ 2005-06-24  0:25       ` James Colannino
  2005-06-24  3:34         ` Eric Bambach
                           ` (2 more replies)
  1 sibling, 3 replies; 14+ messages in thread
From: James Colannino @ 2005-06-24  0:25 UTC (permalink / raw)
  To: linux-c-programming

Eric Bambach wrote:

> Generally speaking (in terms of input validation), its better practice to 
> check against a LEGAL set of characters rather than an illegal set. That way 
> you can get all the characters you need, but everything else is blocked. If 
> you block illegal ones you're bound to miss a few or even ones from extended 
> charsets and input methods that you might not have thought of that could 
> wreck havoc in your program.

Here's what I've whipped up based on your suggestion that I should look
for legal characters instead of the other way around:

<CODE>

/* This function returns 1 if the character being checked is legal and 0
if it isn't. */

int legal_characters(char character_to_check) {

	int index;
	legal_characters[] =
"abcdefghijklmnopqrstuvwxyzAVCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";

	int number_of_legal_chars = sizeof(legal_characters) / sizeof(char);

	for (index = 0; index < number_of_legal_chars; ++index) {
		if (character_to_check == legal_characters[index])
			return 1;
	}

	return 0;

</CODE>

How does this function look?

James
-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

" Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety." --Benjamin Franklin

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-24  0:25       ` James Colannino
@ 2005-06-24  3:34         ` Eric Bambach
  2005-06-24  5:48           ` James Colannino
  2005-06-24  7:57         ` J.
  2005-06-24  8:32         ` Glynn Clements
  2 siblings, 1 reply; 14+ messages in thread
From: Eric Bambach @ 2005-06-24  3:34 UTC (permalink / raw)
  To: James Colannino; +Cc: linux-c-programming

On Thursday 23 June 2005 07:25 pm, James Colannino wrote:
> Eric Bambach wrote:
> > Generally speaking (in terms of input validation), its better practice to
> > check against a LEGAL set of characters rather than an illegal set. That
> > way you can get all the characters you need, but everything else is
> > blocked. If you block illegal ones you're bound to miss a few or even
> > ones from extended charsets and input methods that you might not have
> > thought of that could wreck havoc in your program.
>
> Here's what I've whipped up based on your suggestion that I should look
> for legal characters instead of the other way around:
>
> <CODE>
>
> /* This function returns 1 if the character being checked is legal and 0
> if it isn't. */
>
> int legal_characters(char character_to_check) {
>
>  int index;
>  legal_characters[] =
> "abcdefghijklmnopqrstuvwxyzAVCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";
>
>  int number_of_legal_chars = sizeof(legal_characters) / sizeof(char);
>
>  for (index = 0; index < number_of_legal_chars; ++index) {
>   if (character_to_check == legal_characters[index])
>    return 1;
>  }
>
>  return 0;
>
> </CODE>
>
> How does this function look?

A solution at best. Inneficient at worst. I suppose if you are only comparing 
a few characters it will get you by or if you dont need them processed fast. 
The problem is that for each character you want to validate you have a 
minimum of 1 and a maximum of 65ish loop iterations. That could easily add up 
on a long string. Even a sentence with 30 characters is a minimum of 30 
itterations, max of 1800 with the average probably being a few to several 
hundred.

It does do the job nicely though of returning true if the character is legal 
and you did implement my second suggestion well. Try my code below (not 
tested, might not compile but the general idea is there. Its C++ code because 
of the bool type and the comments, but it could easily be C99 with a little 
editing). See how the whole function collapses to a single line after it is 
first run? You pay for it with a little extra memory but if you need to throw 
hundreds or more characters at the function it will do it fast-It will be 
super-fast no matter how many characters you throw at it. Each charater is 
analyzed by a quick memory jump. The computer only has to execute a few 
instructions per character you pump into the function as opposed to tens to 
hundreds of instructions in your function(compare, jump, add, compare, jump, 
add). There is still a jump at entry to my function each time though because  
I did the lazy initialization. If you want to optimize further hand code the 
table as static and you will see extra performance. 

Perhaps someone can tell me if the compiler is smart enough to optimize out my 
static initialization of the array and collapse it into a single static 
initialization without all the code. I would be very interested in knowing 
that.

e.g. table[256] = { 0,0,1,1,0... };

But maybe im being too pedantic ....your function DOES do the job ;)
I just dont know how you plan to use it.


HTH!

bool legal(unsigned char *character) {

 // legal_characters[] =
 //"abcdefghijklmnopqrstuvwxyzAVCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";
 static unsigned char table[256];
 static bool initialized = false;
 if ( !initialized){
 //Set up the whole table as false
 memcpy(table,0,sizeof(table)); //did i get the memcpy args right? Its been  a 
few months since ive done C/c++
 //Set up 0-9 as true
 for(int j = 48; j <=58;j++){
  table[j] = 1;
 }
 //A-Z is true
  for(int j = 65; j <=90;j++){
    table[j] = 1;
  } 
 //a-z is true
   for(int j = 97; j <=122;j++){
      table[j] = 1;
   } 
        //The stragglers
 table[95] = 1;
 table[45] = 1;
        initialized = true;
  }
  return table[*charater];
}

> James

-- 
----------------------------------------
--EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

                --Alan Cox LKML-December 08,2000 

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-24  3:34         ` Eric Bambach
@ 2005-06-24  5:48           ` James Colannino
  0 siblings, 0 replies; 14+ messages in thread
From: James Colannino @ 2005-06-24  5:48 UTC (permalink / raw)
  To: eric; +Cc: linux-c-programming

Eric Bambach wrote:

>But maybe im being too pedantic ....your function DOES do the job ;)
>I just dont know how you plan to use it.
>  
>

Well for my purposes it's probably ok.  I'll be comparing very short
strings with it, although then again I'll be comparing a good number of
those short strings so the constant checking of those strings will still
add up.  I do like your solution a lot, and actually I think I'll go
ahead and use it :)

James

P.S. Even though it's a small project that I'm working on, it is
something I plan on sharing with others, so as long as you wouldn't have
any objections I'd like to add a comment with your name to the code that
gives you credit for coming up with it.

-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"A well regulated militia being necessary to the security of a free
state, THE RIGHT of the people to keep and bear arms SHALL NOT BE
INFRINGED." --United States Constitution, Second Ammendment


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-24  0:25       ` James Colannino
  2005-06-24  3:34         ` Eric Bambach
@ 2005-06-24  7:57         ` J.
  2005-06-24  8:32         ` Glynn Clements
  2 siblings, 0 replies; 14+ messages in thread
From: J. @ 2005-06-24  7:57 UTC (permalink / raw)
  To: linux-c-programming

On Thu, 23 Jun 2005, James Colannino wrote:

> Eric Bambach wrote:
> 
> > Generally speaking (in terms of input validation), its better practice to 
> > check against a LEGAL set of characters rather than an illegal set. That way 
> > you can get all the characters you need, but everything else is blocked. If 
> > you block illegal ones you're bound to miss a few or even ones from extended 
> > charsets and input methods that you might not have thought of that could 
> > wreck havoc in your program.
> 
> Here's what I've whipped up based on your suggestion that I should look
> for legal characters instead of the other way around:
> 
> <CODE>
> 
> /* This function returns 1 if the character being checked is legal and 0
> if it isn't. */
> 
> int legal_characters(char character_to_check) {
> 
> 	int index;
> 	legal_characters[] =
> "abcdefghijklmnopqrstuvwxyzAVCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";

I'm not going to repeat all the answers you already had, but you know the
#include <ctype.h> ? 

The above [abcdef... ABC. etc..] `man ascii' e.g.

uppercase alpha (c >= 65 && c <= 90) , 
lowercase alpha (c >= 97 && c <= 122),
numerals ... (c >= 48 && c <= 57) etc..

or with ctype.h ... isdigit(), isalnum() .....

#include <stdio.h>
#include <ctype.h>

int legal_characters(char ch) {
 register int retv = -1;

 if(isalpha(ch) || isdigit(ch) || ch == '-' || ch == '_')
  retv = 1;
 else
  retv = 0;

 return retv;
}

int main(void) {
 char c;

 while((c = getchar()) != EOF) {
  if(legal_characters(c))
   putchar(c);
 }

 return 0;
}

You could also use the ctype macro's grouped together in your own defined
macro..

Maybe it's good to point out that the users of your program can also 
be chinese users and other `foreign' users that use different character 
sets that do not fit into the 1 byte character..

Cheers..

J.

> 	int number_of_legal_chars = sizeof(legal_characters) / sizeof(char);
> 
> 	for (index = 0; index < number_of_legal_chars; ++index) {
> 		if (character_to_check == legal_characters[index])
> 			return 1;
> 	}
> 
> 	return 0;
> 
> </CODE>


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-24  0:25       ` James Colannino
  2005-06-24  3:34         ` Eric Bambach
  2005-06-24  7:57         ` J.
@ 2005-06-24  8:32         ` Glynn Clements
  2 siblings, 0 replies; 14+ messages in thread
From: Glynn Clements @ 2005-06-24  8:32 UTC (permalink / raw)
  To: James Colannino; +Cc: linux-c-programming


James Colannino wrote:

> > Generally speaking (in terms of input validation), its better practice to 
> > check against a LEGAL set of characters rather than an illegal set. That way 
> > you can get all the characters you need, but everything else is blocked. If 
> > you block illegal ones you're bound to miss a few or even ones from extended 
> > charsets and input methods that you might not have thought of that could 
> > wreck havoc in your program.
> 
> Here's what I've whipped up based on your suggestion that I should look
> for legal characters instead of the other way around:
> 
> <CODE>
> 
> /* This function returns 1 if the character being checked is legal and 0
> if it isn't. */
> 
> int legal_characters(char character_to_check) {
> 
> 	int index;

> 	legal_characters[] =

Should be preceded by "char", i.e.

	char legal_characters[] =

Better still, make it static and constant:

	static const char legal_characters[] =

> "abcdefghijklmnopqrstuvwxyzAVCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";
                              ^
Typo; should be "B".

> 	int number_of_legal_chars = sizeof(legal_characters) / sizeof(char);
> 
> 	for (index = 0; index < number_of_legal_chars; ++index) {
> 		if (character_to_check == legal_characters[index])
> 			return 1;
> 	}

> How does this function look?

You can replace the loop with a call to strchr(). But using a lookup
table as suggested by Eric would be significantly more efficient.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: comparing char to other known char's
  2005-06-22 23:22 comparing char to other known char's James Colannino
                   ` (3 preceding siblings ...)
  2005-06-23 13:10 ` Adrian Popescu
@ 2005-06-25 11:58 ` HIToC
  4 siblings, 0 replies; 14+ messages in thread
From: HIToC @ 2005-06-25 11:58 UTC (permalink / raw)
  To: James Colannino; +Cc: Linux C programming

On Thursday 23 June 2005 01:22, James Colannino wrote:
> Hey everyone.  I hope this isn't a stupid question.  I've been googling
> around trying to find a function that I can use but haven't been
> successful.  Here's what I want to be able to do:
>
> let's say I have a char called 'character.'  I want to compare
> 'character' to see if it's any one of the characters in a list.  For
> example, maybe I would want to test character to see if it's either 'e',
> 'r', '*', etc.
>
> Is this easy enough to implement?  I could do if (character == 'e' ||
> character == [...] and so on and so forth, but this seems much to
> tedious and unreadable to be my only solution.  If anyone has any ideas
> I'd be extremely grateful :)  Thanks very much in advance.
>
> James


If you prefer a Object Oriented solution, take the example:


/*	char.cpp

	Author:	HIToC
	E-mail:	hitoc_mail@yahoo.it
	Date:	06/25/2005




	Purpose:	Example
*/




#include <iostream>
#include <algorithm>
#include <string>
#include <list>


class	valid_char {
	list<char>	char_list;

public:
	valid_char(const string&);

	bool	find_char(char);
	bool	operator()(char c) { return find_char(c); }

	~valid_char() { }
};

valid_char::valid_char(const string& s)
: char_list()
{
	for(string::const_iterator i = s.begin(); i != s.end(); i++)
		char_list.push_back(*i);
}

bool valid_char::find_char(char c)
{
	if(find(char_list.begin(), char_list.end(), c) != char_list.end())
		return true;
	else return false;
}




int main(int argc, char* argv[])
{
	const char*	alpha = "QWERTY";
	valid_char	vchar(alpha);
	string		str;
	char		c;

	if(argc != 2) {
		cerr <<"Usage: char <string>\n";
		return 1;
	 }

	str = argv[1];
	for(int i = 0; i < str.size(); i++) {
		c = str[i];
		cout <<"Character [" <<c <<"]:\t";
		if(vchar(c)) cout <<"FOUND!\n";
		else cout <<"NOT found!\n";
	}

	return 0;
}


-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2005-06-25 11:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-22 23:22 comparing char to other known char's James Colannino
2005-06-22 23:44 ` David L. Martin
2005-06-22 23:46 ` Eric Bambach
2005-06-23  0:25 ` James Colannino
2005-06-23 13:10 ` Adrian Popescu
2005-06-23 20:40   ` James Colannino
2005-06-23 22:57     ` Eric Bambach
2005-06-23 23:58       ` James Colannino
2005-06-24  0:25       ` James Colannino
2005-06-24  3:34         ` Eric Bambach
2005-06-24  5:48           ` James Colannino
2005-06-24  7:57         ` J.
2005-06-24  8:32         ` Glynn Clements
2005-06-25 11:58 ` HIToC

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.