473,795 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string permutation function

2 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. void string::insert(size_type position, size_type number_of_copies, char c)
  4. {
  5.     string s1 = "CAT";
  6.     string s2 = "MAN";
  7.  
  8.         //add or insert CAT to first 3
  9.         //add or insert MAN to last 3
  10.             //then...
  11.                 //...add TA and insert with CMAN = "TACMAN"
  12.                 //..replace TA with AT and insert with CMAN = "ATCMAN"
  13.                 //..call erase c();
  14.                 //add CT and insert with AMAN = "CTAMAN"
  15.                     //..replace CT with TC and insert with AMAN = "TCAMAN"
  16.                         //..call erase c();
  17.                         //add AC and insert with TMAN = "ACTMAN"
  18.                         //..replace AC with CA and insert with TMAN = "CATMAN"
  19.  
  20.  
  21. //postcondition: The specified number of copies of c have been 
  22. //inserted into the string at the indicated position. 
  23. //Existing characters that used to be at or after the given position have been shifted right 
  24. //one spot 
  25. }
  26. void string::erase(size_type position, size_type n)
  27. {
  28.  
  29.     // delete c[]; or c[] = NULL;
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35.     char c[5];
  36.  
  37.  
  38.   insert(,,); //First function call, so it starts at one        
  39. }
  40.  
Please give me ideas and suggestions. I need help in putting my idea into code as this is all new to me.

Thank you.
Nov 13 '07 #1
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.
Nov 13 '07 #2
peace357
2 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string.h>
  4. #include <string>
  5. #include <iomanip> 
  6.  
  7. using namespace std;
  8.  
  9. /*void string::insert(size_type position, size_type number_of_copies, char c)
  10. {
  11.     string string1 = "CAT";
  12.     string string2 = "MAN";
  13.         permute ("", "CAT");
  14.         permute ("", "MAN");
  15.     cout<<string1 + string2; //results in error.
  16.                                                     //how to use insert?
  17.  
  18.  
  19. }
  20. void string::erase(size_type position, size_type n)
  21. {
  22.     //dont know what to do
  23. }*/
  24. void permute( string prefix, string s )
  25. {
  26.     if ( s.size() <= 1  )
  27.         cout <<prefix <<s <<"\n";
  28.     else
  29.         for ( char *p = s.begin(); p <s.end(); p++ )
  30.         {
  31.             char c = *p;
  32.             s.erase( p );
  33.             permute( prefix + c, s );
  34.             s.insert( p, c );
  35.         }
  36.  
  37. }
  38. int main()
  39. {
  40.     permute( "CAT", "MAN");
  41.     return 0;      
  42. }
  43.  
Please help me! I am drowning!
Nov 14 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
5644
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
1
623
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
3
2964
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"
20
41308
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
12
3231
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...
3
3140
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.
2
5899
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
16
3069
by: saki | last post by:
Write a program to print all the permutations of a given string.
13
2246
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):
0
9672
marktang
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...
0
9519
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,...
0
10214
jinu1996
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...
0
9042
agi2029
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...
0
6780
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();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.