Question

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

-------------------------------------------------------------------------------

#include <iostream>
#include <algorithm>

using namespace std;

// Checking if two strings are permutation
bool permutationcheck(string in1, string in2)
{
// Length of both of the strings
int n1 = in1.length();
int n2 = in2.length();

if (n1 != n2)
return false;

// Sorting
std::sort(in1.begin(), in1.end());
std::sort(in2.begin(), in2.end());

// comparing now that they are sorted
for (int i = 0; i < n1; i++)
if(in1[i] != in2[i])
return false;

return true;
}

int main()
{
string in1;
string in2;
cout << "Enter two strings: " << endl;
cin >> in1 >> in2;

if(permutationcheck(in1, in2))
cout << "Yes" << endl;
else
cout << "No" << endl;

return 0;
}
-------------------------------------------------------------------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <algorithm>

using namespace std;

bool unique_chars(string str){
   int n = str.length(), i, j;
   for(i = 0;i<n;i++){
      for(j = 0;j<i;j++){
         if(i!=j && str[i] == str[j]){
            return false;
         }
      }
   }
   return true;
}

// Checking if two strings are permutation
bool permutationcheck(string in1, string in2)
{
// Length of both of the strings
int n1 = in1.length();
int n2 = in2.length();

if (n1 != n2)
return false;

// Sorting
std::sort(in1.begin(), in1.end());
std::sort(in2.begin(), in2.end());

// comparing now that they are sorted
for (int i = 0; i < n1; i++)
if(in1[i] != in2[i])
return false;

return true;
}

int main()
{
string in1;
string in2;
cout << "Enter two strings: " << endl;
cin >> in1 >> in2;

if(permutationcheck(in1, in2)){
   cout << "Yes" << endl;
   if(unique_chars(in1)){
      cout<<"All unique characters"<<endl;
   }
   else{
      cout<<"String does not have all unique characters"<<endl;
   }
}
else
cout << "No" << endl;

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...
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
  • C++ XOR two strings. I am trying to XOR two strings 00112233445566778899AABBCCDDEEFFAABB060606060606 and 4ca00fd6dbf1fb284ca00fd6dbf1fb284ca00fd6dbf1fb28 my program...

    C++ XOR two strings. I am trying to XOR two strings 00112233445566778899AABBCCDDEEFFAABB060606060606 and 4ca00fd6dbf1fb284ca00fd6dbf1fb284ca00fd6dbf1fb28 my program is giving me: SPTWPVSPT[X   q'&t'!"u#'t~u"#rPTTTVVT as a result However, when i go to http://xor.pw/? and input the two hexadecimal strings in I get the result: 4cb12de59fa49d5fc439a56d172c15d7e61b09d0ddf7fd2e what am I doing wrong in my program below? // plaintext value here string pthex = "00112233445566778899AABBCCDDEEFFAABB"; // IV value here string sHex = "4ca00fd6dbf1fb28"; string PaddedPlainText = pthex + "060606060606"; //added the pad //cout<<PaddedPlainText<<endl; // the above...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • 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...

  • Write this program in c++:

    test.txtLab7.pdfHeres the main.cpp:#include "Widget.h"#include <vector>#include <iostream>#include <string>#include <iomanip>#include <fstream>using std::ifstream;using std::cout;using std::cin;using std::endl;using std::vector;using std::string;using std::setprecision;using std::setw;bool getWidget(ifstream& is, Widget& widget){ string name; int count; float unitCost; if (is.eof())  { return false; } is >> count; is >> unitCost; if (is.fail())  { return false; } is.ignore(); if (!getline(is, name))  { return false; } widget.setName(name); widget.setCount(count); widget.setUnitCost(unitCost); return true;}// place the definitions for other functions here// definition for function mainint main(){ // Declare the variables for main here  // Prompt the...

  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • In C++: You are to write two functions, printString() and testString(), which are called from the...

    In C++: You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints every character in the string to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains characters that are in sorted order, false otherwise. You may assume that all characters are lowercase and only alphabetical characters are present. See the main() to see how the two functions are...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • When I try to run the program it gives me an erron saying Expected expresion on...

    When I try to run the program it gives me an erron saying Expected expresion on this line, else if(phrase.find(sub_String)!=std::string::npos). #include #include #include using namespace std;    int main( ) {          // Defining variables. //Initilizing variables. int magicNum; int magicNum1; int position; int phraseLenght; string vowels = "aeiou"; string phrase; string sub;       // The phrase and the sub phrase cout << "Enter a phrase:" << endl; std::getline (std::cin,phrase);    cout << "Enter a 3-character sub:"...

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