user3064203
user3064203

Reputation: 101

Substring in C++

I am writing a program that will calculate password strength according to two formulas. One of these formulas requires me to extract certain parts of the password, first character, next seven, next 12, and all remaining characters. I am having trouble extracting all the remaining characters.

I am unsure how what to put in the upper bounds for the substring method. Here is my code.

Any help would be greatly appreciated!

cout << "Now please enter a password of 20 characters or more (including spaces!):" << endl;
getline(cin, twenty_password);
cout << "What character set is being used?";
cin >> character_set_20;
twenty_length = twenty_password.length();
twenty_ent_strength = (twenty_length*((log(character_set_20)) / (log(2))));
first_char_twenty = twenty_password.substr(0, 1);
next_seven_twenty = twenty_password.substr(1, 7);
next_twelve = twenty_password.substr(7, 19);

Upvotes: 0

Views: 194

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

first_char_twenty.clear();
next_seven_twenty.clear();
next_twelve.clear();
remaining.clear();

if ( twenty_password.size() >= 1 )
{
   first_char_twenty = twenty_password.substr(0, 1);
}

if ( twenty_password.size() >= 2 )
{
   next_seven_twenty = twenty_password.substr(1, 7);
}

if ( twenty_password.size() >= 9 )
{
    next_twelve = twenty_password.substr( 8, 12);
}

if ( twenty_password.size() >= 21 )
{
   remaining = twenty_password.substr( 20 );
}

Upvotes: 0

pm100
pm100

Reputation: 50110

lastbit = password.substring(25);

will return substring from 25 up till end of string

Upvotes: 0

sehe
sehe

Reputation: 392863

Just leave the upper bound out

http://en.cppreference.com/w/cpp/string/basic_string/substr

basic_string substr( size_type pos = 0,
                      size_type count = npos ) const;

You'll see that the default value is npos, meaning the remainder of the string.

(Hold on to that website! It answers your questions much faster than we do)

Upvotes: 4

Related Questions