MHB Tomato or Potato? Output Comparison

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
Click For 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: 86
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
1K
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K