+ 2

An alternative method ... ?

how to do the same sort of sorting as below with unsorted characters instead of numbered elements? https://code.sololearn.com/c0iaaItw6j28/?ref=app

3rd Aug 2017, 3:17 AM
Rahul George
Rahul George - avatar
13 Réponses
+ 10
That's a whole different story. The first idea which comes to mind is a library which acts as a dictionary. The second would be for the program to read from a text file containing a whole dictionary of vocabulary, and then cross checking your string literal / char array content with the literals read from the file. (The file should be pre-sorted, alphabetical order, and based on literal length). Once a literal from the dictionary is found to match all the characters in the array provided, print the literal, instead of sorting the array.
3rd Aug 2017, 6:38 AM
Hatsy Rei
Hatsy Rei - avatar
3rd Aug 2017, 3:49 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
For now, just store the pre-defined words in a 'dictionary' array. std::string dict[] = {"celebrate", "test", "random", ... }; When you have "cleebaret", compare this literal with all elements inside dict. Length is key. The second step is to sort your array using the method I introduced to "abceeelrt". Do this for the dict element as well. After sorting both your literal and the dict element, compare them. If they are equal, print the original dict element. I'll try to code a sample later.
3rd Aug 2017, 7:08 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
I don't do well in C++ but try. Comment what each line does and what several lines do as a whole. You might get inspired.
3rd Aug 2017, 3:56 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
// Didn't even add a single line. #include <iostream> using namespace std; int main() { char x[200], t; int size; cout <<"\n Enter required no of elements to be sorted: "; cin>>size; cout<<size<<endl; // cout <<"enter array of unsorted elements of size "<<size<<" : \n"; for (int i=0;i<size;i++) cin >>x[i]; cout <<"\n Original array: \n"; cout<<endl; for (int i=0;i<size;i++) cout <<x[i]<<"\t"; cout<<"\n"; //Bubble sort for(int i=0;i<size;i++) { for(int j=0;j<size-1-i;j++) { if(int(x[j])>int(x[j+1])) { t=x[j]; x[j]=x[j+1]; x[j+1]=t; } } } cout <<"\n Sorted Array: \n"; cout<<endl; for (int i=0;i<size;i++) { cout << x[i] <<"\t"; } cout <<endl; return 0; }
3rd Aug 2017, 5:56 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
It should be something like this: https://code.sololearn.com/ca77dOVrVRxw/?ref=app
3rd Aug 2017, 7:46 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
@hatsy thanks for the tip. @$Vengat sure I'll do that
3rd Aug 2017, 4:11 AM
Rahul George
Rahul George - avatar
+ 2
https://code.sololearn.com/cT6wBJf3fFVc/?ref=app what next? how to do the ASCII part? *haven't seen such a long error message in a long time*
3rd Aug 2017, 4:38 AM
Rahul George
Rahul George - avatar
+ 2
you don't need strcpy() for sorting the letters in a char array, you could treat them as you treated the numbers in the other program.
3rd Aug 2017, 5:04 AM
lion
lion - avatar
+ 2
@hatsy file handling doesn't work completely well in code playground. how to use the data library?
3rd Aug 2017, 6:51 AM
Rahul George
Rahul George - avatar
+ 1
@Hatsy if you could check out the comments section of my char sorting code, you'll understand that what I want is the rearrangement of the muddled char input(s) to give a meaningful string as output. for example, if I enter "c l i e b a r e t" as the input, it should return " c e l e b r a t e " and not " a b c e e e l r t " ( according to ASCII ).
3rd Aug 2017, 6:24 AM
Rahul George
Rahul George - avatar
+ 1
@Hatsy for the file version: I'd sort the strings based on their sorted letters. For example, rainbow would be before lion because abinorw is before ilno alphabetically. I think that would speed a binary search better than sorting by length (I'm not sure).
3rd Aug 2017, 7:23 AM
lion
lion - avatar
0
if you want to sort strings, you treat them just like you treated the numbers, I think. If you want to work with c-strings (char arrays), see strcmp() function.
3rd Aug 2017, 4:16 AM
lion
lion - avatar