linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: HIToC <hitoc_mail@yahoo.it>
To: James Colannino <james@colannino.org>
Cc: Linux C programming <linux-c-programming@vger.kernel.org>
Subject: Re: comparing char to other known char's
Date: Sat, 25 Jun 2005 13:58:53 +0200	[thread overview]
Message-ID: <200506251323.31271.hitoc_mail@yahoo.it> (raw)
In-Reply-To: <42B9F2C7.2030205@colannino.org>

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

      parent reply	other threads:[~2005-06-25 11:58 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200506251323.31271.hitoc_mail@yahoo.it \
    --to=hitoc_mail@yahoo.it \
    --cc=james@colannino.org \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).