C++ String Compare: Alphabetize Strings

In summary: System.out.println("both strings are the same!");}In summary, the code tries to print the second string first if it is bigger than the first string, and prints the first string if they are the same.
  • #1
ineedhelpnow
651
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
  • #2
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.
 
  • #3
(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)
 
  • #4
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)
 
  • #5
thats what I am stuck on. what if I am given the other string first. how do i get that to come first?
 
  • #6
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.
 
  • #7
so my if condition was valid but it was wrong because they were switched up? :confused:
 
  • #8
ineedhelpnow said:
so my if condition was valid but it was wrong because they were switched up? :confused:

Yes. (Nod)
 
  • #9
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);
}
 

FAQ: C++ String Compare: Alphabetize Strings

How do I compare two strings in C++?

To compare two strings in C++, you can use the compare() function from the string library. This function takes in two strings as parameters and returns an integer value. If the first string is less than the second string, the function returns a negative value. If the first string is greater than the second string, the function returns a positive value. And if both strings are equal, it returns 0.

How do I alphabetize strings in C++?

To alphabetize strings in C++, you can use the sort() function from the algorithm library. This function takes in two iterators and a comparison function as parameters. The comparison function can be a built-in function like less() or a custom function that compares two strings based on your specific criteria.

How can I ignore case sensitivity when comparing strings in C++?

To ignore case sensitivity when comparing strings in C++, you can use the strcasecmp() function from the cstring library. This function takes in two strings as parameters and returns an integer value, similar to the compare() function. However, strcasecmp() ignores the case of the characters while comparing.

What if I want to compare strings based on a specific order rather than alphabetical?

In such cases, you can create a custom comparison function that takes in two strings and compares them based on your desired criteria. For example, you can compare strings based on their length, numerical value, or any other specific order. Then, you can pass this function as a parameter to the sort() or compare() function to perform the comparison.

Are there any other built-in functions for string comparison in C++?

Yes, there are other built-in functions for string comparison in C++, such as equal(), mismatch(), and lexicographical_compare(). These functions offer different ways to compare strings and are useful for specific scenarios. You can refer to the C++ documentation for more details on these functions.

Similar threads

Replies
1
Views
2K
Replies
118
Views
8K
Replies
22
Views
3K
Replies
8
Views
2K
Replies
40
Views
3K
Replies
10
Views
2K
Replies
1
Views
1K
Replies
5
Views
4K
Back
Top