+ 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!
22 ответов
+ 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
+ 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.
+ 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.
+ 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?
+ 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?
+ 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
+ 1
I’m having trouble of sorting
+ 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?
+ 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.
+ 1
https://code.sololearn.com/ctC5wnC9B5BI/?ref=app
i know it’s all confusing. this is just part of my whole code.
+ 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.
+ 1
Hey, it worked! Thank you for helping me Hatsy Rei
<3
0
i should tell you that the strings(listed names) are from a txt file so my
ifstream inputStream;
ofstream outputStream;
0
yes, it is not allowed for this task.
0
on what part should i replace for it?
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
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
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?
0
i have the functions of
strcpy
strcmp
strlength