Question

Multi Part question for c++ thank you and yes I'm going to rate you up thanks...

Multi Part question for c++ thank you and yes I'm going to rate you up thanks ^^

Part A

Question #1

Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string.

Question #2

If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters.

Hint: For comparing strings you can use functions like length(), strcmp(), or write your own check_equality(string, string) function.

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

// C++ program to check if two strings are permutation of each other or not, and if they are check if the strings have unique characters

#include <iostream>

#include <string>

using namespace std;

// function declaration

bool check_equality(string str1, string str2);

int main() {

               string str1, str2;

               // input of both strings

               cout<<" Enter string 1 : ";

               cin>>str1;

               cout<<" Enter string 2 : ";

               cin>>str2;

               bool result = check_equality(str1,str2);

               bool unique = true;

               if(result) // if strings are permutation of each other

               {

                              cout<<" The two string are permutation of each other"<<endl;

                              int count=0;

                              // loop to get the count of each character in str1

                              for(size_t i=0;i<str1.length();i++)

                              {

                                             count=0;

                                             for(size_t j=0;j<str1.length();j++)

                                                            if(str1.at(i) == str1.at(j))

                                                                           count++;

                                             if(count > 1) // if count > 1, then non-unique characters

                                             {

                                                            unique = false;

                                                            break;

                                             }

                              }

                              if(unique)

                                             cout<<" Strings have unique characters"<<endl;

                              else

                                             cout<<" Strings do not unique characters"<<endl;

               }else

                              cout<<" The two string are not permutation of each other"<<endl;

               return 0;

}

// function to check if two strings are permutation of each other or not

bool check_equality(string str1, string str2)

{

               int count1, count2;

               if(str1.length() != str2.length()) // check if length of both strings are same

                              return false;

               // loop over string 1

               for(size_t i=0;i<str1.length();i++)

               {

                              count1=0, count2 = 0;

                              // count the frequency of character at ith position in str1 in str1

                              for(size_t j=0;j<str1.length();j++)

                                             if(str1.at(i) == str1.at(j))

                                                            count1++;

                              // count the frequency of character at ith position in str1 in str2

                              for(size_t j=0;j<str2.length();j++)

                                             if(str2.at(j) == str1.at(i))

                                                            count2++;

                              // if count doesn't match, then they are not permutation of each other

                              if(count1 != count2)

                                             return false;

               }

               return true; // strings are permutation of each other

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Multi Part question for c++ thank you and yes I'm going to rate you up thanks...
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
  • Multi part question c++ and yes I'm going to rate you up thank youu! Part A...

    Multi part question c++ and yes I'm going to rate you up thank youu! Part A Ask the user to input a string. Then sort the string in reverse lexicographic order and print it out. This would be done using the integer values of the characters (ASCII table). Use any one of the sorting methods. For example: if the user enters "zeta" sorting it in reverse lexicographic order would give "ztea". if the user enters "ZETA" it would give "ZTEA"....

  • Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...

    Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...

  • Can someone please help me with this code? I'm writing in C++. Thank you in advance....

    Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers). In order to complete this, you will need a couple of new functions. First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline: string question; cout <<...

  • i need this written in C, not C++ or anything else, just C, thank you. apparently...

    i need this written in C, not C++ or anything else, just C, thank you. apparently this works, but I can't use this, hope this helps though: #include<iostream> #include<string.h> using namespace std; int main() { char finalist1[10], finalist2[10], winner[10]; char decision[3][10]; int count1=0,count2=0,i; cout<<"Enter name of the first finalist: "; gets(finalist1); cout<<"\nEnter name of the second finalist: "; gets(finalist2); cout<<"\n\nEnter decision of the first committee member: "; gets(decision[0]); if(strcmp(decision[0],finalist1)==0 || strcmp(decision[0],finalist2)==0) { cout<<"\nEnter decision of the second committee member: ";...

  • ** Language Used : Python ** PART 2 : Create a list of unique words This...

    ** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...

  • C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪...

    C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪ Writing a while loop ▪ Write functions and calling functions Text Processing [50 points] We would like to demonstrate our ability to control strings and use methods. There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It...

  • Please note that I cannot use the string library function and that it must be coded...

    Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text in the first problem from “Please enter a string of maximum 30 characters:” to “Enter string: ”. Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade. 2.Comments: Header comments...

  • Use c-strings for the following project: Write a C++ program that declares an array containing up...

    Use c-strings for the following project: Write a C++ program that declares an array containing up to a maximum of 20 sentences, each sentence of maximum 81 characters long, using c-strings. Continue reading sentences from the user and store them in the array of sentences, until the user enters a NULL string for the sentence or 20 sentences have been entered. Then, one by one, display each sentence entered by the user and present the following menu of operations on...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two...

    Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two strings while ignoring the case of alphabetical characters. Your program will take two strings as input from the user and use your function to compare them. Assume a maximum C-string size of 1000 characters. Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your...

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
ADVERTISEMENT