What's wrong with my code? I'm tring to find a way to compare 2 strings and if have all the characters in two string, with the same quantity, will be valid, otherwise, invalid. The conditions are:
1. User type 2 string (with different characters and quantity, example: str1 = asd || str2: asdfg)
2. Code compares str2 to str1 char by char, im using strN.at(); being N the numeber of the string.
3. Once a character is found in str1 the position can't be reviewed. The same to str2, once a character of str2 was found in str1, the positions can't be reviewed.
4. If "asd" was found in str2, it is a valid arument.
5. If reach to the end of the str2, with no success of comparison, will be invalid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
int main(){
std::string str1;
std::string str2;
int i = 1;
int j = 1;
std::cout << "\nPlease, insert the first string:";
std::getline (std::cin, str1);
std::cout << "\nPlease, insert the second string:";
std::getline (std::cin, str2);
std::cout << "\nYou chose: " << str1 << " how string 1 and: " << str2 << " how string 2.";
do{
if(str2.at(j) == str1.at(i)){
i++;
j = 1;
}
else{
i++;
}
}while(i < str1.size() || j < str2.size());
if(i < str1.size()){
std::cout << "\nValid Argument.\n\n";
}
else{
std::cout << "\nInvalid Argument.\n\n";
}
return 0;
}
|