Question

Sorting usually follows the standard alphabet, with digits less than any letter. In this problem letters,...

Sorting usually follows the standard alphabet, with digits less than any letter. In this problem letters, digits, and the space character are jumbled in a special way. Write a C++ program to sort a set words according to the jumbled alphabet. Input from the keyboard the jumble order of letters, digits and the spaces. Next, input one word per line the words to be sorted until a . (period) is entered. Output the sorted words according to the jumbled alphabet in order one per line. Make sure to convert output to all capital letters. Use the C++ string class. Refer to the sample output below.

Sample Run:
Enter the alphabet to sort by: QWERTYUIOPASDFGHJKLZXCVBNM
Enter a word: PICK

Enter a word: KEYBOARD

Enter a word: PICKLE

Enter a word: zebra

Enter a word: KEYS

Enter a word: ANTEATER

Words sorted in jumble alpha order:

PICK

PICKLE

ANTEATER

KEYS

KEYBOARD

ZEBRA

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

// assign each character a value based on sequence
map<char, int> getCharValues(string sequence)
{
map<char, int> charValue;
// assign a value to each char in sequence from 0 to len of sequence
for (int i = 0; i < sequence.length(); i++)
{

charValue[sequence[i]] = i;
}
// return values
return charValue;
}
// to check which string value if greater
bool isGreater(string s1, string s2, map<char, int> charValues)
{
// first we find the length of the smallest string
int min = s1.length() < s2.length() ? s1.length() : s2.length();
// now iterate through each letter
for (int i = 0; i < min; i++)
{
// check if value of any of the s1's character is greater than s2's
if (charValues[s1[i]] > charValues[s2[i]])
{
// return true if it is;
return true;
}
// check if value of any of the s2's character is greater than s1's
else if (charValues[s1[i]] < charValues[s2[i]])
{
// return false if it is
return false;
}
else
{
// if the value of both s1's char and s2's char equal then continue to check
continue;
}
}

// get greater value based on lenght
if (s1.length() > s2.length())
{
return true;
}
else
{
return false;
}
}

// sort vector of words
void sortWords(vector<string> &words, map<char, int> charValues)
{
// sort using bubble sort
for (int i = 0; i < words.size(); i++)
{
for (int j = 0; j < words.size() - 1 - i; j++)
{
if (isGreater(words[j], words[j + 1], charValues))
{
swap(words[j], words[j + 1]);
}
}
}
}

int main(int argc, char const *argv[])
{ //ask for a sequence to sort by
string sequence;
cout << "Enter the alphabet to sort by: " << endl;
cin >> sequence;
// create an vector of words(dynamic array)
vector<string> words;
string word;
// taking input from user until a '.' if entered
cout << "Enter a Word: ";
cin >> word;
while (word != ".")
{
// convert each letter of the word to upper case
std::for_each(word.begin(), word.end(), [](char &c) {
c = ::toupper(c);
});
// store word into vector
words.push_back(word);
cout << "Enter a Word: ";
cin >> word;
}
// get char value using getCharValues
map<char, int> charValues = getCharValues(sequence);
// sort words vector
sortWords(words, charValues);

// print content of words after sorting

for (string word : words)
{
cout << word << endl;
}

return 0;
}

screenshot:

output:

if you like the answer, please upvote and for any queries ask in the comment :)

Add a comment
Know the answer?
Add Answer to:
Sorting usually follows the standard alphabet, with digits less than any letter. In this problem letters,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT