Question

ANSWER IN C++ ONLY A string of characters including only alphabets (lowercase letters) is provided as...

ANSWER IN C++ ONLY

A string of characters including only alphabets (lowercase letters) is provided as an input.

  • The first task is to compute the frequency of each character appearing in the string. In the output, the characters have to be arranged in the same order as they appear in the input string.
  • Then characters have to be rearranged, such that all the characters having a specific frequency, say xx, come together.

Let the frequency of a character, lying in between the two characters having same frequency xx, be yy. The steps to be followed for getting the desired arrangement are as follows:

  • If y>xy>x, then shift the character at the end.
  • If y<xy<x, then shift the character at the beginning.

Input:

  • Line 1 contains the string of characters without spaces.
  • Line 2 contains an integer N.
  • Line 3 contains N integers separated by space, N1 N2 … NN. These are the specific frequency values whose corresponding characters should come together.

Output:

  • Line 1 has characters and their frequency as computed originally. Each output value is separated using a space.
  • Each of the following N lines contains the rearranged sequence of characters and their frequency for the given N specific frequencies. Each line in the output has output values separated using a space.

Constraints:

  • Number of characters in the string ranges in between 1 and 1000.

Sample Input:

nomatterhowbusyyoumaythinkyouareyoumustfindtimeforreadingorsurrenderyourself
toselfchosenignorance
3
3 1 6

Sample Output:

n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 w 1 b 1 u 7 s 6 y 6 i 5 k 1 f 4 d 3 g 2 l 2 c 2
k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 d 3 g 2 l 2 c 2 u 7 s 6 y 6 i 5 f 4
k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 d 3 g 2 l 2 c 2 u 7 s 6 y 6 i 5 f 4
c 2 l 2 g 2 d 3 h 3 k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 s 6 y 6 i 5 f 4 e 10 r 10 u 7

EXPLANATION:

First line has characters and their frequency as computed using the given input string. The characters are arranged in the same order as per their appearance in the original string. Second line is obtained after rearranging characters appearing thrice together. Similarly, Third line is generated after placing characters that are coming only once together. The Third line output is similar to the Second line output as targeted characters (having frequency one) automatically comes in continuation during the previous rearrangement corresponding to frequency three. Lastly, the Fourth line presents the final character and frequency sequence after putting characters with frequency 6 together.

PLS HELP

Homework Answers

Answer #1

First part

using namespace std;

  

#define SIZE 26

  

// function to print the character and its frequency

// in order of its occurrence

void printCharWithFreq(string str)

{

// size of the string 'str'

int n = str.size();

  

// 'freq[]' implemented as hash table

int freq[SIZE];

  

// initialize all elements of freq[] to 0

memset(freq, 0, sizeof(freq));

  

// accumulate freqeuncy of each character in 'str'

for (int i = 0; i < n; i++)

freq[str[i] - 'a']++;

  

// traverse 'str' from left to right

for (int i = 0; i < n; i++) {

  

// if frequency of character str[i] is not

// equal to 0

if (freq[str[i] - 'a'] != 0) {

  

// print the character along with its

// frequency

cout << str[i] << freq[str[i] - 'a'] << " ";

  

// update frequency of str[i] to 0 so  

// that the same character is not printed  

// again

freq[str[i] - 'a'] = 0;

}

}

}

  

// Driver program to test above

int main()

{

string str = "nomatterhowbusyyoumaythinkyouareyoumustfindtimeforreadingorsurrenderyourselftoselfchosenignorance";

printCharWithFreq(str);

return 0;

}

Trying to figure out others too if you like then pls upvote, meanwhile I am working on other subparts of your question

Know the answer?
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
Similar Questions
(C++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on using arrays. Instructions Create a program called arrays.c that does the following: 1. Prompt the user for a string (<= 100 characters). (using the attached file: AS06Data.txt) AS06Data.txt file contains the text " Instructions: When your executing program displays "Enter string 1:" starting with T, select the line of text below, copy with ctrl-c, then type ctrl-v THIS IS a test of string 0123456789...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
This is for C++ A string is a palindrome if reverse of the string is same...
This is for C++ A string is a palindrome if reverse of the string is same as the original string. For example, “abba” is palindrome, but “abbc” is not palindrome. Basically a string with a plane of symmetry in the middle of it is considered a palindrome. For example, the following strings are palindromic: "abbbbba", "abba", "abbcbba", "a", etc. For this problem, you have an input string consisting of both lowercase or uppercase characters. You are allowed to pick and...
Write a program that takes a string of characters (including spaces) as input, computes the frequency...
Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but mostly readable way called “leet” – short for “elite”. In essence, it was a simple character replacement algorithm, where a single “regular” character was replaced by one or more “leet” characters; numbers remained the same. Here’s one of the most readable versions: a 4 g 9 m /\\/\\ s $ y ‘/ b B h |-| n |\\| t 7 z Z c (...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT