0

I'm writing Email Validator at the moment and I want to check if the string contains any SYMBOL(not digit or character) except '-','_','.', '@'. If it not contains,I have to raise the variable called "counter".

This is what I tried so far:

for (int i = 0; i < mail.length(); i++) {
        if ((mail[i] >= 'A' && mail[i] <= 'Z') || (mail[i] >= 'a' && mail[i] <= 'z') || (mail[i] >= '0' && mail[i] <= '9') || (mail[i] == '.' || mail[i] == '-') || (mail[i] == '@' || mail[i] == '_')) {
            if(i == mail.length()) {
                counter+=1;
                break;
            } 
        }
    }
getsadzeg
  • 640
  • 3
  • 9
  • 19

2 Answers2

3

You can just check if there is symbol not from your set:

size_t pos = mail.find_first_not_of( "-_.@" );
if( pos != std::string::npos )
    ++counter;

This code increase counter once if whole string has any symbol not from that set, ie it is based on problem description not your code, that tries to count how many symbols not from that set.

Slava
  • 43,454
  • 1
  • 47
  • 90
0

You can use regular expressions using boost. If I understood your question correctly, you want to check for strings not containing letters, digits and "-_.@". An example implementation is shown below. Adapt the code to your needs:

// std
#include <iostream>
using namespace std;
// boost
#include <boost/regex.hpp>

int main () {
    unsigned int counter = 0;
    // ... my code ...
    string myString = "foo%1@.com";
    boost::regex myRegex("[^a-zA-Z_.@-]+");
    if (boost::regex_search(myString, myRegex)) {
        counter++;
    }
   // ... my code ...
    return 0;
}

Update

In order to compile the code, you need to install the boost library. You do not need to install it all. Just make sure to install libboost-regex. For compilation, you may use the following command which links with the necessary boost library:

g++ -o main main.cpp -lboost_regex
homeless
  • 336
  • 1
  • 8
  • Please give me a link of the library files that you used. – getsadzeg Feb 06 '15 at 15:28
  • Yeah,but I have an error : error LNK1104: cannot open file 'libboost_regex-vc120-mt-gd-1_57.lib'. Can you tell me,what error is this? I've added the boost library into the VS 2013. I saw your update,but I'm at Windows OS. – getsadzeg Feb 06 '15 at 15:45
  • It's been a while seen I used VC. There are many possible reasons. I guess you added the libraries correctly. If you have doubts, have a look at this video (until minute 3 is fine) which shows how to add a library https://www.youtube.com/watch?v=4MdTqNDrPSY. The example uses OpenCV but you can easily adapt it to your needs. – homeless Feb 06 '15 at 16:01
  • The next thing, I would check is the problem with the whitespaces. See the first answer on this page http://stackoverflow.com/questions/133698/why-does-fatal-error-lnk1104-cannot-open-file-c-program-obj-occur-when-i-c . You need to make sure in the **Additional Dependencies** property window to put the links containing spaces in quotes `"..."`. In the video, this window is shown in minute 2:36. – homeless Feb 06 '15 at 16:02
  • Thanks a lot. I've fixed the error. The problem was I didn't change platform toolset of boost's libraries. – getsadzeg Feb 07 '15 at 14:41