anonghost
anonghost

Reputation: 17

How does strings comparison in C++ work?

I am trying to solve this problem.

I am implementing it with strings. Here is my code snippet

string s,ss;
// s and ss both contains integer input.

while(s <= ss )
//while( s<=ss && s.size() <= ss.size())
{
    int i = inc, j = dec;   // inc and dec are middle values. both equal if odd else different

    while((s[j]-'0')==9 && i < len && j>=0){
        // for cases like 999
        s[i] = s[j] = '0';
        i++;
        j--;
    }
    if(j<0){
        s = "1" + s;
        int l = s[len-1] - '0';
        l++;
        //cout<<l<<"\n";
        s[len] = (l + '0');
    }
    else{
        int l = s[j] - '0';
        l++;
        s[i] = s[j] = (l+'0');
    }

    if(s <= ss)
        cout<<"out in wild "<<s<<" and "<<ss<<"\n";
}

cout<<s<<endl;

The problem that I am facing is when input is like 999 or 9999. The outer while loop keeps on looping even when the value of s increases, but if I add while( s<=ss && s.size() <= ss.size()) it works completely fine. Why is while(s<=ss) is not working? I rarely use the string class, so I don't understand it completely. Why don't string s= 101 and ss=99 stop the while loop?

Complete code link is here

Upvotes: 0

Views: 222

Answers (3)

quantdev
quantdev

Reputation: 23813

You are comparing strings with lexicographical order, not numbers , so "101" is less than "99" (because '1' < '9') , e.g.

int main(){
    std::string s = "99";
    std::string ss = "101";
    
    std::cout << std::boolalpha << (s <= ss);  
}

Outputs false.

Notes:

  • A better design for your program would be to manipulate numbers (int or double ...) and not strings in the first place, so this kind of expressions would naturally work as you expect.

    E.g. "101" + "99" is "10199", not "200" ...

  • But if you really need strings, consider this post to sort strings containing numbers.

  • As pointed by @Deduplicator, a program that needlessly overuses strings is sometimes called Stringly Typed

  • Also see std::lexicographical_compare


Since your input explicitly only involves positive integers without leading 0, writing a comparison function is trivial, something like : (untested)

/* Returns 1 if the integer represented by s1 > the integer represented by s2
*  Returns -1 if the integer represented by s1 < the integer represented by s2
*  Return 0 is both are equals
*
*  s1 and s2 must be strings representing positive integers without trailing 0
*/
int compare(const std::string& s1, const std::string& s2)
{
  if(s1.size() > s2.size())
      return 1;
  if(s2.size() > s1.size())
      return -1;
      
  for(std::size_t i = 0 ; i < s1.size() ; ++i)
  {
    if(s1[i] - '0' < s2[i] - '0')
      return 1;
    if(s2[i] - '0' < s1[i] - '0')
      return -1;
  }
          
  return 0;
}

Upvotes: 4

c000p
c000p

Reputation: 53

As the s is compared with ss in lexicographical order, I would suggest you to compare one char from tail with one char from head (one by one till you reach the middle) to solve that problem.

Upvotes: 0

Odiseo
Odiseo

Reputation: 21

While s and ss are string variables, they are compared character by character.

In the case that you mentioned being: s = "101" & ss = "99", by first hand it will check the first character in each string, and as '1' < '9' it exit up with s < ss. I would advise you to convert those values to integers before comparison.

Upvotes: 0

Related Questions