function to check for valid characters in a string

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 11022 Views - Last Post: 02 February 2011 - 12:38 PM Rate Topic: -----

#1 radicaledward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

function to check for valid characters in a string

Posted 31 January 2011 - 02:30 PM

Hi. I'm working on a homework assignment and I'm trying to write a function that searches a string for characters contained in another string, and says if the original string has invalid characters or not. For example it would search "thisisanemail" and if invalid characters would found it would return false. I know I'm creating a bool function that much is obvious. I also know how to search a string from beginning to end. The only thing I'm having trouble with is how to check the string against a string of valid characters while searching it from beginning to end. If anyone could help me out with this it would be much appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: function to check for valid characters in a string

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: function to check for valid characters in a string

Posted 31 January 2011 - 02:39 PM

Do you need a C or C++ solution?

C-strings or std::string?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 radicaledward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: function to check for valid characters in a string

Posted 31 January 2011 - 05:05 PM

A C++ answer please.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3988
  • View blog
  • Posts: 12,302
  • Joined: 25-December 09

Re: function to check for valid characters in a string

Posted 31 January 2011 - 06:25 PM

For std::string check out the std::string.find() functions: string.

If you are not using std::string then check out the functions in cstring.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 radicaledward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: function to check for valid characters in a string

Posted 31 January 2011 - 07:27 PM

Thanks I know about these functions but my teacher actually said we can't use any of these for the assignment so I've been trying to create my own to accomplish this task and have been drawing blanks.
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: function to check for valid characters in a string

Posted 31 January 2011 - 07:52 PM

loop though each element and check each charter.
size_t size = str.size();
size_t subsize = sub.size();
for(unsigned int i=0;i<size;++i) {
    if(str[i]==somthing) {
        //do somthing
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: function to check for valid characters in a string

Posted 31 January 2011 - 09:58 PM

You have to specify what is considered "valid". If the intent of the lesson is the get you to understand character arrays, then great. However, doing this in practice, say in a web application where you have to validate user input from a form or something, you will have to deal with unicode, and that is totally different (i.e. a lot harder).

Anyway, in ishkabible's example above, "something" needs to be however you are going to determine if the current character is valid. I can think of about 3 or 4 ways to do that right off, but teacher's usually have some restrictions as to what you can and can not use (I never understood why though...)

You probably need to do some range checking with if(), or a fancy way would be a lookup table.
Was This Post Helpful? 2
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: function to check for valid characters in a string

Posted 01 February 2011 - 09:07 AM

You might code something like this ...

const string notValidStr = ":;.,?!";
bool char_is_in( char c, const string& nv );
bool chars_all_valid( const string& testStr )
{
   for( int i = testStr.size()-1; i >= 0; --i )
      if char_is_in( testStr[i], notValidStr ) return false;
   return true;
}
bool char_is_in( char c, const string& nv )
{
   // ...
}


This post has been edited by David W: 01 February 2011 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#9 radicaledward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: function to check for valid characters in a string

Posted 01 February 2011 - 09:33 PM

Ok understand the loop obviously running through the string I'm searching, but what I really don't understand is how if the current character is invalid how that character can be checked against a whole string simultaneously.
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: function to check for valid characters in a string

Posted 01 February 2011 - 11:00 PM

View Postradicaledward, on 01 February 2011 - 11:33 PM, said:

Ok understand the loop obviously running through the string I'm searching, but what I really don't understand is how if the current character is invalid how that character can be checked against a whole string simultaneously.


bool char_is_in( char c, const string& nv )
{
   // ...
}



You need to fill in the above with a loop that compares c against each char in string nv
and return true if there is a match
otherwise ... return false

so then you have a loop within a loop when your outer calling function calls the inner function

:)
Was This Post Helpful? 0
  • +
  • -

#11 radicaledward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: function to check for valid characters in a string

Posted 02 February 2011 - 11:30 AM

Ok thanks a lot for explaining that. I'm still kinda confused though about one aspect. This is what I have now:
bool CheckForValidCharacters(char* emailPart, char* validCharacters)
{
	int x = 0;
	while (x < (GetLength(emailPart))
	{
		int y = 0;
		while (y < (GetLength(validCharacters))
		{
			if (emailPart[x] == validCharacters[y])









I'm kind of confused about how to write this if statement. I was thinking I could just assign a value if it was true or false and then take that value at the end of the loop to see if character was invalid. Is there an easier way to accomplish this task? Thanks.

Actually I just thought of something regarding that post. I'm also confused about that loop because if implementing that way the loop could return a value that is not necessarily an invalid character but is just not equal to the current position of the valid character array? Or am I just second guessing myself?
Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: function to check for valid characters in a string

Posted 02 February 2011 - 11:34 AM

View Postradicaledward, on 31 January 2011 - 04:30 PM, said:

Hi. I'm working on a homework assignment and I'm trying to write a function that searches a string for characters contained in another string, and says if the original string has invalid characters or not. For example it would search "thisisanemail" and if invalid characters would found it would return false. I know I'm creating a bool function that much is obvious. I also know how to search a string from beginning to end. The only thing I'm having trouble with is how to check the string against a string of valid characters while searching it from beginning to end. If anyone could help me out with this it would be much appreciated. Thanks.

Are you looking for invalid characters ... !?
Was This Post Helpful? 0
  • +
  • -

#13 radicaledward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: function to check for valid characters in a string

Posted 02 February 2011 - 11:38 AM

Well here's the thing. I'm looking for invalid characters, but the way my teacher defined the function is as follows. "Searches the string and returns false if string contains characters that are not found in valid characters". So I'm kind of confused about that.
Was This Post Helpful? 0
  • +
  • -

#14 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: function to check for valid characters in a string

Posted 02 February 2011 - 11:41 AM

so you teacher did not say what is a valid charter and what isn't? how dose your teacher expect you to do the assignment? i would be willing to bet that you went over it in class; don't you remember anything about valid vs. non valid charters?

This post has been edited by ishkabible: 02 February 2011 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#15 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: function to check for valid characters in a string

Posted 02 February 2011 - 11:46 AM

View Postradicaledward, on 02 February 2011 - 01:38 PM, said:

Well here's the thing. I'm looking for invalid characters, but the way my teacher defined the function is as follows. "Searches the string and returns false if string contains characters that are not found in valid characters". So I'm kind of confused about that.

Then just adjust your function code for that not very different problem :)
For each char c in stringToTest
   If that char c not in validStr return false // you are done

// if reach here
Otherwise return true


This post has been edited by David W: 02 February 2011 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2