MHB Tomato or Potato? Output Comparison

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
AI Thread Summary
When two strings are compared using relational operators in C++, they are evaluated lexicographically based on the ASCII values of their characters. In the provided example, if "tomato" is entered first and "potato" second, the output will be "potato" followed by "tomato" because "potato" is lexicographically greater than "tomato". Similarly, when comparing "aabaa" and "aaaza", "aabaa" is considered less than "aaaza" because the comparison starts from the first differing character, where 'b' in "aabaa" has a lower ASCII value than 'z' in "aaaza". Thus, the output will be "aabaa" followed by "aaaza". This understanding of string comparison is crucial for correctly predicting outputs in C++ programming.
needOfHelpCMath
Messages
70
Reaction score
0
If “tomato” get entered first and “potato” get input afterward, what will the output be?

Code:
string input1;
string input2;

cin>>input1;
cin>>input2;

if (input1 < input2) {
	cout<<input1<<endl;
	cout<<input2<<endl;
}
else {
	cout<<input2<<endl;
	cout<<input1<<endl;
}
 
Last edited:
Technology news on Phys.org
First, when composing a post, you will notice a toolbar directly above the text area, and in this toolbar is a button that will generate [CODE][/CODE] tags:

View attachment 5298

You can either click that button first, and then paste your code in between them, or you can paste your code into the post content, then select your code, and click the button to have the selected text wrapped with the tags.

This makes your source code stand out and easier to read since white space is preserved, allowing the indentation to be retained. Please edit your post to add these tags. ;)

Okay, when 2 strings are being compared using the relational operators, how exactly are they being compared? For example, can you tell me which of the following two strings is the greater in such a comparison?

  • aabaa
  • aaaza
 

Attachments

  • codetoolbar.png
    codetoolbar.png
    18.5 KB · Views: 81
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top