String Comparison

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;
    
}
For starters, j is one forever and never changes.
Last edited on
You mean, i can't change J in the first IF?
My compiles aren't running the DO...WHILE, he just execute until std::cout << "\nYou chose: " << str1 << " how string 1 and: " << str2 << " how string 2.";
You mean, i can't change J in the first IF?


I meant you're not changing j. Ever. You set it to one and you don't change it.
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
38
#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{
          j++;
       }
   }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;
    
}


Like this?

But at the same, my compiler continues announcing warnings on the lines 26 and 28, so didn't running, the warning is "comparison between signed and unsigned integer expressions."
Last edited on
Maybe should I put the sizes of the strings in a int var, or just unsign the var?
Last edited on
Why do you have two index values (i and j) at all? Since you're comparing corresponding values, you can use the same index.

Check for unequal sizes first. That way your loop doesn't have to worry about whether the sizes are equal.

string and array indexes start at 0, not 1, so the first character to compare is at index 0.
I'm usign two index 'cause this:

[g][a][s] --> str1
[a][s][d][f][g] --> str2

first [a] of the str2 must compare with [g], so, invalid, next should compare to [a], so, valid.

The values compared was:
str2.at(0) != str1.at(0);
str2.at(0) == str1.at(1);

in your logic, I just compare equal values like:
str2.at(0) != str1.at(0);
str2.at(1) != str1.at(1);
str2.at(2) != str1.at(2);

right?
Right. Unless I misunderstand the problem. You're just trying to determine if the strings are equal right? To do that, just compare corresponding characters of the strings. If you find two that aren't equal then the strings aren't equal.
Topic archived. No new replies allowed.