Sort string by length C++
Can anyone help me how to write a code for sorting a list of names from a txt file by length? Just the function and implementation. For example: input: 1 Angel 2 Lucifer 3 Bob 4 Valentina Output: 3 Bob 1 Angel 2 Lucifer 4 Valentina *i can only use the <iostream>, <fstream> libraries and nothing else. Thanks!
1/31/2019 5:37:11 AMlakas
22 AnswersNew Answer
OK, this is an example of sorting strings based on their length. Bubble sort is used. https://code.sololearn.com/cF8C73tc1wd8/?ref=app https://www.sololearn.com/learn/650/?ref=app
O...kay, if that is the case, you may need to use strlen() to get the length of the c-style string. http://www.cplusplus.com/reference/cstring/strlen/ Instead of names[j].length(), do strlen(names[j]). However, this requires <cstring>, which also contradicts the requirements. Please give me some time.
Fine, if you seriously want to use array of arrays of characters to represent string arrays, there is still a way to compare and swap (sort) them... Few minutes please.
Try this? Charmaine Labastida https://code.sololearn.com/cF8C73tc1wd8/?ref=app
The length of strings can be obtained using the .length() method. You can then be able to use any sorting algorithm of your choice to sort the strings. Which part are you having problems with, specifically? Reading the strings into string arrays? Sorting?
Character arrays are not classes and hence do not possess the .length method. Using sizeof() instead of .length may not work because arrays are already fixed in size. Are string arrays forbidden in this task, and you are tied to arrays of character arrays?
Charmaine Labastida Please refer to this code again. I made some changes according to your code and the requirements of the task. https://code.sololearn.com/cF8C73tc1wd8/?ref=app
I’m having trouble of sorting
the code looks great, but the .length is giving me an error saying request for member of non-class type. is there anyway to write this?
Can I see your code snippet which is giving you that error? You appear to be calling .length on something else which isn't a string.
https://code.sololearn.com/ctC5wnC9B5BI/?ref=app i know it’s all confusing. this is just part of my whole code.
If that's the case, the assignment is literally impossible, since array identifiers are essentially pointers which point to the first element of the array blocks.
Hey, it worked! Thank you for helping me Hatsy Rei <3
i should tell you that the strings(listed names) are from a txt file so my ifstream inputStream; ofstream outputStream;
yes, it is not allowed for this task.
on what part should i replace for it?
unfortunately it’s not allowed to use pointers either. Is it possible to just use for loops to have an ascending ouput(example above for the ouput)? The “names.txt” contains the list of the names. For this section of sorting, the assignment are: •sorts the list of names by-length •prints the list of sorted-by-length names to an output file(SortedByLength.txt), preceded by their original order in the input file
the “Names.txt” has the list of names 1 Angel 2 Lucifer 3 Bob 4 Valentina So, for the output, it needs to be in an ascending format according to its string’s length and still has it’s original number, like this: 3 Bob 1 Angel 2 Lucifer 4 Valentina
is there anyway that i can send you a screen shot of my whole code so it would be much easy instead of typing it all here?
i have the functions of strcpy strcmp strlength