C/C++ C++ String Compare: Alphabetize Strings

AI Thread Summary
The discussion focuses on how to print two strings in alphabetical order using C++. A user expresses difficulty with their implementation, particularly with using if-else statements to compare the two strings. They initially attempt to print the strings based on a comparison but realize their logic is reversed, leading to incorrect output. Participants emphasize the importance of showing effort in problem-solving and provide guidance on correcting the if-condition to ensure the strings are sorted correctly. The conversation highlights the learning process, with the user ultimately achieving the desired outcome after adjusting their code. The importance of understanding string comparison and the need for proper conditional logic is underscored throughout the exchange.
ineedhelpnow
Messages
649
Reaction score
0
Print the two strings in alphabetical order. Assume the strings are lowercase. End with newline. Sample output:

capes rabbits
Sample program:

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
   string firstString;
   string secondString;

   firstString = "rabbits";
   secondString = "capes";

   <STUDENT CODE>

   return 0;
}
i tried a few different ways but it won't work. do i need to use if else statements?
 
Technology news on Phys.org
ineedhelpnow said:
i tried a few different ways but it won't work.

What did you try? (Wondering)
You're supposed to show some effort! (Doh)

do i need to use if else statements?

Edit: yes, you do.
 
(Crying) sorry. there's been a lot of effort on this side though. trust me.

ok here's what i tried for the if and else statements
Code:
if (secondString > firstString) {
    cout << secondString << " " << firstString << endl;
}
else {
    cout << firstString << " " << secondString << endl;
}
the program responded with heck no

and then i tried
Code:
secondString > firstString;
cout << secondString << " " << firstString << endl;
it passed partially but failed other tests. and i can remember what else i tried :confused:

do i use switch statements (Wondering)
 
Much better!
Now I have at least something to respond to! (Wasntme)

ineedhelpnow said:
ok here's what i tried for the if and else statements
Code:
if (secondString > firstString) {
    cout << secondString << " " << firstString << endl;
}
else {
    cout << firstString << " " << secondString << endl;
}
the program responded with heck no

Let's take a look at what the first part does.

If the second string comes later in the alphabet than the first string, then print the second string first.

Does that sound right if you want to print them alphabetically sorted? (Wondering)
and then i tried
Code:
secondString > firstString;
cout << secondString << " " << firstString << endl;
it passed partially but failed other tests. and i can remember what else i tried :confused:

Well, since there is no "if", the code is always going to do the same thing.
It will first print whatever secondString is, and then it will print what firstString is.

What does that give on the output? (Wondering)
 
thats what I am stuck on. what if I am given the other string first. how do i get that to come first?
 
ineedhelpnow said:
thats what I am stuck on. what if I am given the other string first. how do i get that to come first?

My point is that your if-condition is the wrong way around.
It sorts the two strings alphabetically in reverse. :eek:
Otherwise you're good to go.
 
so my if condition was valid but it was wrong because they were switched up? :confused:
 
ineedhelpnow said:
so my if condition was valid but it was wrong because they were switched up? :confused:

Yes. (Nod)
 
heck yeah! it worked! ilu ils!
 
  • #10
ineedhelpnow said:
heck yeah! it worked! ilu ils!

Yeah! (Cool)

See how it helps if you show what you tried? :rolleyes:
 
  • #11
no you could have just told me the answer (Blush) lol kidding. you're right. thanks
 
Last edited:
  • #12
ineedhelpnow said:
no you could have just told me the answer (Blush)

Nope.
It's not for nothing that we have a rule that posters are expected to show some effort.
I myself take pleasure in helping someone learn. (Sun)
Merely giving out answers doesn't achieve that. (Doh)
 
  • #13
if (secondString.compareTo(firstString) <= 0){
System.out.println(secondString + " " + firstString);
}
else if (firstString.compareTo(secondString) <= 0){
System.out.println(firstString + " " + secondString);
}
 

Similar threads

Replies
1
Views
2K
Replies
22
Views
3K
Replies
8
Views
2K
Replies
40
Views
3K
Replies
1
Views
1K
Back
Top