C++ String Compare: Alphabetize Strings

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Compare String
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming task that involves comparing two strings and printing them in alphabetical order. Participants explore various approaches to implement this functionality, including the use of conditional statements.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares an initial attempt to compare two strings using if-else statements but expresses uncertainty about the correctness of their approach.
  • Another participant questions the effort shown and asks for details on what was tried, emphasizing the importance of demonstrating effort in problem-solving.
  • A participant describes their if-else logic but indicates that the output was incorrect, leading to confusion about the order of the strings.
  • Some participants suggest that the if-condition may be reversed, which could lead to incorrect sorting of the strings.
  • There is a discussion about the necessity of showing work to receive help, with some participants expressing frustration at not receiving direct answers.
  • A later post introduces a different method using the compareTo function, indicating an alternative approach to string comparison.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of the initial if-else statements, with some agreeing that the logic was flawed while others clarify that the condition was valid but misapplied. The discussion remains unresolved regarding the best approach to implement the string comparison.

Contextual Notes

Participants do not fully agree on the correct implementation of the string comparison, and there are mentions of partial successes and failures in testing the proposed solutions.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in string manipulation and conditional logic.

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 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K