I am trying to write two recursive functions involving two strings, 1)CAT & 2)MAN.
My function needs to print out:
TACMAN
ATCMAN
CTAMAN
TCAMAN
ACTMAN
CATMAN
Two functions required are:
1) void string::insert( size_type position, size_type number_of_copie s, char c);
2) void string::erase(s ize_type position, size_type n)
Other functions I may need:
3) Do I need to use a permute function and a dopermute function? How do I make the permute functions part of my program by using insert and erase? -
using namespace std;
-
-
void string::insert(size_type position, size_type number_of_copies, char c)
-
{
-
string s1 = "CAT";
-
string s2 = "MAN";
-
-
//add or insert CAT to first 3
-
//add or insert MAN to last 3
-
//then...
-
//...add TA and insert with CMAN = "TACMAN"
-
//..replace TA with AT and insert with CMAN = "ATCMAN"
-
//..call erase c();
-
//add CT and insert with AMAN = "CTAMAN"
-
//..replace CT with TC and insert with AMAN = "TCAMAN"
-
//..call erase c();
-
//add AC and insert with TMAN = "ACTMAN"
-
//..replace AC with CA and insert with TMAN = "CATMAN"
-
-
-
//postcondition: The specified number of copies of c have been
-
//inserted into the string at the indicated position.
-
//Existing characters that used to be at or after the given position have been shifted right
-
//one spot
-
}
-
void string::erase(size_type position, size_type n)
-
{
-
-
// delete c[]; or c[] = NULL;
-
}
-
-
-
int main()
-
{
-
char c[5];
-
-
-
insert(,,); //First function call, so it starts at one
-
}
-
Please give me ideas and suggestions. I need help in putting my idea into code as this is all new to me.
Thank you.
2 3004 RRick 463
Recognized Expert Contributor
The key ideas here are: permutation and recursion. This means your insert routine must call itself. In your example, it doesn't. Take a look at the visit routine in the following link for an idea of what this means. http://www.bearcave.com/random_hacks/permute.html
In your example, only the first string is being permutated. The second one is simply added to the end of the various permutations. One idea is to keep these separate until you need to print them.
As to why you need two recursive functions, this seems to be based on a specific type of permutation algorithm. I'm not sure why you need to do this. Once again, the above link shows an example.
I have figured out the "permutatio n" part and I need help with "recursion" part. Here are the conditions for my functions:
1) the stopping case of the function occurs when the length of first has zero characters
2) for void string::insert: -The specified number of copies of c have been inserted into the string at the indicated position. -Existing characters that used to be at or after the given position have been shifted right one spot.
3) for void string::erase: -n characters have been removed from the string, beginning at the specified position. -
#include <iostream>
-
#include <cstdlib>
-
#include <string.h>
-
#include <string>
-
#include <iomanip>
-
-
using namespace std;
-
-
/*void string::insert(size_type position, size_type number_of_copies, char c)
-
{
-
string string1 = "CAT";
-
string string2 = "MAN";
-
permute ("", "CAT");
-
permute ("", "MAN");
-
cout<<string1 + string2; //results in error.
-
//how to use insert?
-
-
-
}
-
void string::erase(size_type position, size_type n)
-
{
-
//dont know what to do
-
}*/
-
void permute( string prefix, string s )
-
{
-
if ( s.size() <= 1 )
-
cout <<prefix <<s <<"\n";
-
else
-
for ( char *p = s.begin(); p <s.end(); p++ )
-
{
-
char c = *p;
-
s.erase( p );
-
permute( prefix + c, s );
-
s.insert( p, c );
-
}
-
-
}
-
int main()
-
{
-
permute( "CAT", "MAN");
-
return 0;
-
}
-
Please help me! I am drowning!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Talin |
last post by:
I'm sure I am not the first person to do this, but I wanted to share
this: a generator which returns all permutations of a list:
def permute( lst ):
if len( lst ) == 1:
yield lst
else:
head = lst
for x in permute( lst ):
yield head + x
|
by: user |
last post by:
Hello
I have Array of 50 ints. I want to receive random permutation, so
in each int will be different number from 0-49.
Is there any class for permutation ?
Thanx
Michal
|
by: Csaba Gabor |
last post by:
I'm comparing the text of (snippets of) web pages which I expect to be
quite different or quite similar. In the case where they are similar,
I would like to display the more recent one and say something like:
Word 2 added : "Jack be nimble"
Words 10-11 changed to: "the quick brown fox"
: "the brown fast quick fox"
Words before word 20 removed: "sat in a corner on"
|
by: anurag |
last post by:
hey can anyone help me in writing a code in c (function) that prints
all permutations of a string.please help
|
by: Pascal |
last post by:
hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way without success.
for example if the number is 123 456 : i would like to have some random
strings like theese :
(12 * 10 000) + (345 * 10) + (6*1)
or
(123*1 000)+(4*100)+(5*10)+(6*1)
etc...
| |
by: weidongtom |
last post by:
Hi,
I have been working at this problem, and I think I need a permutation
algorithm that does
the following:
Given a list of elements that are either a character or a character
follows by a number, e.g.
|
by: Assimalyst |
last post by:
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the dictionary essentially creates the following array:
Key Value
|
by: saki |
last post by:
Write a program to print all the permutations of a given string.
|
by: sillyhat |
last post by:
Hello, can someone please help.
I found the following code at http://code.activestate.com/recipes/252178/
def all_perms(str):
if len(str) <=1:
yield str
else:
for perm in all_perms(str):
for i in range(len(perm)+1):
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |