+ 3

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!

31st Jan 2019, 5:37 AM
lakas
22 Antworten
+ 5
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
31st Jan 2019, 5:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
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.
31st Jan 2019, 6:38 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
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.
31st Jan 2019, 7:18 AM
Hatsy Rei
Hatsy Rei - avatar
31st Jan 2019, 7:35 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
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?
31st Jan 2019, 5:42 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
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?
31st Jan 2019, 6:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
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
31st Jan 2019, 6:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
I’m having trouble of sorting
31st Jan 2019, 5:43 AM
lakas
+ 1
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?
31st Jan 2019, 6:12 AM
lakas
+ 1
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.
31st Jan 2019, 6:14 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
https://code.sololearn.com/ctC5wnC9B5BI/?ref=app i know it’s all confusing. this is just part of my whole code.
31st Jan 2019, 6:26 AM
lakas
+ 1
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.
31st Jan 2019, 7:13 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Hey, it worked! Thank you for helping me Hatsy Rei <3
31st Jan 2019, 8:08 AM
lakas
0
i should tell you that the strings(listed names) are from a txt file so my ifstream inputStream; ofstream outputStream;
31st Jan 2019, 6:34 AM
lakas
0
yes, it is not allowed for this task.
31st Jan 2019, 6:35 AM
lakas
0
on what part should i replace for it?
31st Jan 2019, 6:41 AM
lakas
0
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
31st Jan 2019, 7:06 AM
lakas
0
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
31st Jan 2019, 7:10 AM
lakas
0
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?
31st Jan 2019, 7:18 AM
lakas
0
i have the functions of strcpy strcmp strlength
31st Jan 2019, 7:20 AM
lakas