C++ String Compare: Alphabetize Strings

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Compare String
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
12 replies · 15K views
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?
 
Physics news on Phys.org
(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:
 
heck yeah! it worked! ilu ils!
 
no you could have just told me the answer (Blush) lol kidding. you're right. thanks
 
Last edited:
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)
 
if (secondString.compareTo(firstString) <= 0){
System.out.println(secondString + " " + firstString);
}
else if (firstString.compareTo(secondString) <= 0){
System.out.println(firstString + " " + secondString);
}