Answer to Question #13260 in C++ for busisiwe skosana

Question #13260
how do i go about writting this program it says here, write a program that displays the folloing menu:
1. input a word
2. display all susbstrings
3. display all permutations
4. Exit

a user can not choose options 2 & 3 without first choosing option 1

include a function called Substrings that prints all substrings, sorted by length, e.g if the user inputs "rum", the program prints :
r
u
m
ru
um
rum

include a function called permutations that displays all possible permutations of a given input string --if the string contains duplicate characters, you may have multiple repeated results.

e.g : If the input is cat

Output:
cat
cta
act
atc
tac
tca

either string or characters or both datatypes can be used in your solution.try to make its work for a sentence as well .plz help
1
Expert's answer
2012-08-17T10:30:30-0400
#include "stdafx.h"
#include <iostream>
#include
<conio.h>
#include <string>

using namespace std;
void
Substrings(string word);
int Permutations(char* set, int begin, int
end);
void swap(char* src, char* dst);
int main()
{
string
word;

int ch=0;
do{
cout<<"1. input a
word\n";
cout<<"2. display all susbstrings \n";
cout<<"3.
display all permutations\n";
cout<<"4. Exit\n";
cout<<"Select
one item: ";
cin>>ch;
switch(ch){
case 1:
cout<<"Input a
word: ";
cin >> word;
break;
case
2:
Substrings(word);
break;
case 3:
char str[80]={0} ;
int
arrayLength = word.length();
for (int i = 0; i <arrayLength;
i++){
str[i] = word[i];
}

Permutations(str,
0,arrayLength);

break;
}
}while(ch!=4);
return 0;
}
void
Substrings(string word){
string arrayofwords[500];
int count=0;
for(int
j=0;j<word.size()-1;j++){
for (int i = 0; i < word.size()+1;
i++){
arrayofwords[count]=word.substr(j,i);
count++;
}
}
for(int
k=0;k<count;k++){
cout << arrayofwords[k] << "\n";
}

}
int Permutations(char* set, int begin, int end){
int i;
int range
= end - begin;
if (range == 1) {
printf("%s\n", set);
} else
{
for(i=0; i<range; i++) {
swap(&set[begin],
&set[begin+i]);
Permutations(set, begin+1, end);
swap(&set[begin],
&set[begin+i]);

}
}
return 0;

}
void swap(char* src,
char* dst)
{
char ch = *dst;
*dst = *src;
*src = ch;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS