Question

In: Computer Science

Write a program that takes a string from the user, identifies and counts all unique characters...

Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions.

For character identification

Develop a program that takes a string argument, and returns an array containing all unique characters.

For character counting

Develop a program that takes an array returned from above function as an argument along with the given string and return an array containing the total count of each uniquely identified character present in the argument array.

(Dev Cpp +Multifing )

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;

//function to calculate unique Characters
vector<char> uniqueCharactersFunc(string str){
    
    // map to store and keep track of distinct characters
    unordered_map<char, int> mp;
    for(int i = 0; i<str.length(); i++){
        // iterate the string and store it into map inorder to know the count of each character in the string
        mp[str[i]]++;
    }
    
    // vector of char typr to return the answer
    vector<char>ans; 
    for(auto it = mp.begin(); it!=mp.end(); it++){
        // filling th vector with desired result
        ans.push_back(it->first);
    }
    return ans;
}

// function to calculate unique Characters count
vector<pair<char, int> > countOfUniqueCharactersFunc(vector<char>uniqueCharacters, string str){
    
    //map to iterate and store 
    unordered_map<char,int>mp;
    for(int i = 0; i<str.length(); i++){
        mp[str[i]]++;
    }
    
    //create vector to store the unique Characters and its count in the given string
    vector<pair<char, int> > countOfUniqueCharacters;
    for(int i = 0; i<uniqueCharacters.size(); i++){
        countOfUniqueCharacters.push_back(make_pair(uniqueCharacters[i], mp[uniqueCharacters[i]]));
    }
    return countOfUniqueCharacters;
}

int main(){
    string inputString;
    cin >> inputString;
    
    if(inputString.length() == 0){
        cout << "Empty String" << endl;
        return 0;
    }
    
    // array of unique characters
    vector<char> uniqueCharacters = uniqueCharactersFunc(inputString);
    
    // print the unique characters
    for(int i=0; i<uniqueCharacters.size(); i++){
        cout << uniqueCharacters[i] << " ";
    } cout << endl;
    
    // count of unique characters
    vector<pair<char, int> > countOfUniqueCharacters = countOfUniqueCharactersFunc(uniqueCharacters, inputString);
    
    // print the count of unique characters
    for(int i=0 ; i<countOfUniqueCharacters.size(); i++){
        cout << countOfUniqueCharacters[i].first << "->" << countOfUniqueCharacters[i].second << endl;
    }
    return 0;
}

Related Solutions

Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Python Program 1: Write a program that takes user input in the form of a string...
Python Program 1: Write a program that takes user input in the form of a string Break up the string into a list of characters Store the characters in a dictionary The key of the dictionary is the character The value of the dictionary is the count of times the letter appears Hint: remember to initialize each key before using Output of program is the count of each character/letter Sort the output by key (which is the character) Python Program...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also. Hint: You can invoke the isalnum() function on a character, and it will return True...
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a program that counts the letters in a given string and then display the letter...
Write a program that counts the letters in a given string and then display the letter count in order from high to low. The program should: Display a message stating its goal Prompt the user to enter a string input Count the letters in the string (hint: use dictionaries) Display the letter count in order from high to low (sort your letter count) For example, for the input Google the output should be G - 2 O - 2 E...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT