frank17
frank17

Reputation: 107

using str::find and find_first_of and last_of

Im not sure if I used find_first of and last_of correctly, but what I want to do is that I need to print error when it finds _ on the beginning or at the end of my code, plus when it finds two or more _ next to each other, like this lol___, lol__lol, _lol_, _lol, lol_, and one more rule, _ cannot be in front of capital letter, like this lol_Lol

here is my code

             std::string::size_type n;
             std::string::size_type n2;
             std::string::size_type n3;
             std::string const ss = slovo;

             n  = ss.find('_');
             n2 = ss.find_first_of('_');
             n3 = ss.find_last_of('_');

            if (n2 == std::string::npos && n3 == std::string::npos) {
            cout << "chyba" << endl;
            }else if(n == std::string::npos){

                  string s = transform(slovo);
                  cout << s << endl;

                  }else{
                  string s = untransform(slovo);
                  cout << s << endl;

                  }

EDIT>

if ( !ss.empty() && ss.front() == '_' && ss.back() == '_' )
               {
                cout << "Chyba" << endl;
               } else {

                if ( ss.length() > 3 && ss.find( '__', 1, ss.length() - 2 ) != std::string::npos )
                {

                 if (n == std::string::npos){

                      string s = transform(slovo);
                      cout << s << endl;

                      }else{
                      string s = untransform(slovo);
                      cout << s << endl;

                      }
                }else{
            cout << "chyba" << endl;

         } 
         }

EDIT2:

cin >> slovo;
             }      
             std::string::size_type n;
             std::string const ss = slovo;

             n  = ss.find('_');

             // kontrola podtrznika

           if ( ss.empty() && ss[0] == '_' && ss[ss.length() - 1] == '_' )
           {
            cout << "chyba" << endl;
            }

           if ( ss.length() > 3 && ss.find( "__", 1, ss.length() - 2 ) != std::string::npos )
           {
            cout << "chyba" << endl;
            }

             if (n == std::string::npos)
                 {
                  string s = transform(slovo);
                  cout << s << endl;

                  }else{
                  string s = untransform(slovo);
                  cout << s << endl;
                 }

Upvotes: 1

Views: 2816

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

It is obvious that the two function calls will give you the same result

         n  = ss.find('_');
         n2 = ss.find_first_of('_');

In this context they are equivalent.

If I have understood you correctly you need to determine whether the first and the last characters in a string are underscores and whether the string contains two adjacent undescores within itself.

To determine the first case it is simple to write

if ( !ss.empty() && ss.front() == '_' && ss.back() == '_' )
{
//...
} 

To find at least two adjacent underscores excluding the first and the last characters you can write

if ( ss.length() > 3 && ss.find( "__", 1, ss.length() - 2 ) != std::string::npos )
{
//...
}

Upvotes: 0

Tom Tanner
Tom Tanner

Reputation: 9354

if those functions return npos it means the character couldn't be found in the string. So if one of them returns that, all 3 of them will.

So this code outputs 'chyba' it there's no _ in the string or calls untransform otherwise. From your description, that's not what you intend.

You really need to scan through the string for all those conditions. And if you want to check the first and last characters of the string, use ss[0] and ss[ss.length() - 1] (taking appropriate care you don't have a string of length 0 or 1).

Upvotes: 2

Related Questions