Tomato or Potato? Output Comparison

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on string comparison in C++ using relational operators. When two strings, such as "tomato" and "potato", are inputted, the program outputs them in lexicographical order based on their ASCII values. The code provided utilizes the 'cin' function for input and conditional statements to determine the order of the strings. Additionally, participants explore how strings like "aabaa" and "aaaza" are compared, emphasizing the importance of understanding character encoding in string operations.

PREREQUISITES
  • Understanding of C++ syntax and data types
  • Familiarity with the 'cin' and 'cout' functions for input and output
  • Knowledge of relational operators in programming
  • Basic concepts of ASCII values and lexicographical order
NEXT STEPS
  • Research C++ string manipulation techniques
  • Learn about character encoding and its impact on string comparison
  • Explore advanced C++ features like the STL string class
  • Study sorting algorithms and their implementation in C++
USEFUL FOR

Programmers, computer science students, and anyone interested in understanding string operations and comparisons in C++.

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: 88

Similar threads

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