Jump to content

c++ permutation list

So i've been faffing about with an issue for the day. i'm trying to write program that will take a word say "apple" and output all permutations of that word in capitals and lowercase and I've gotten nowhere with it.

There are two parts, that i can distinguish, in this program. and that's 1. identify how many letters are in this word and 2. work out how to permute it.

http://pastebin.com/u2HyGW9R here's what I have so far not that it is by no means complete, i'm stuck and I don't know how to work this out.

Link to comment
Share on other sites

Link to post
Share on other sites

C++ has a function std::next_permutation that will return the...next permutation. However to get all permutations it expects you to start with sorted input. There's an example at the bottom of that link.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to do it yourself, recursion is the easiest way.
 

void myFunc(char* str, int len, int pos)
{
	// check if done
	if (pos >= len)
	{
		cout << str << endl;
		return;
	}

	// Solve the rest given the origianl character
	myfunc(str, len, pos+1);

	// Invert the case of the current char and solve the rest
	str[pos] ^= 0x20;
	myfunc(str, len, pos+1);

	return;
}

the arguments to the initial call would be (in order):

A copy of your original string (as it will be modified in place), the value returned by strlen(), and 0 since you're starting at the beginning.

 

If you're using a string object, use a reference, but still send a copy as the first argument (as it will be modified).

The beginning of the code would then be:

void myFunc(string& str, int pos)
{

	if (pos >= str.length())
	{
		cout << str << endl;
		return;
	}
	...

 

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, fizzlesticks said:

C++ has a function std::next_permutation that will return the...next permutation. However to get all permutations it expects you to start with sorted input. There's an example at the bottom of that link.

I think there might have been a miscommunication or unclarity from my end, judging from what I can understand from this function this will scramble the input string, which is not my goal. my goal is to take an input string and have it feed out all combinations of capital and lower case letters of that string so if i were to feed it "apple" it will return "apple Apple aPple APple etc."

Link to comment
Share on other sites

Link to post
Share on other sites

I suppose another thing I can do is take the total length of the word (in the case of apple it'd be 5) and work out the total combination of end results which would be 2length of the word so in the case of apple since apple has 5 letters it'd be 25 (32) outputs and make a sort of table in binary and work through all inputs starting with (again using the case of apple) 00000 where every 0 represents a lowercase letter and every 1 being capital and compare the digits of the binary to the characters.

Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream>

int main(){
	
	std::string word;
	std::cout << "Word: "; //Read word
	std::cin >> word;
	
	for(size_t i = 0; i < word.length(); i++){ //Convert to lowercase word.
		if(word[i] >= 'A' && word[i] <= 'Z'){ 
			word[i] ^= 0x20;
		} else if(word[i] < 'a' || word[i] > 'z'){ // Additional check, allowing for non letter characters would involve some skipping in interating it later on.
			std::cout << "\"" << word[i] << "\" is not a letter." << std::endl;
			
			return 1;
		}
	}
	
	size_t count = 1 << word.length(); // You already figuered this out, number of possible combinations.
	
	std::cout << "Combinations: " << std::endl << word << std::endl; // Print out first combination which is all lowercase.
	
	for(int i = 0; i < count - 1; i++){		
		for(size_t p = 0; p < word.length(); p++){ // Go through letters and...
			word[p] ^= 0x20; // Swap letter case.
			
			if(word[p] < 'a'){ //If letter was lowercase and we have switched it to uppercase then break.							
				break;
			}
			
			// It works like adding binary 1
		}
		
		/*		
		You can accomplish it with do-while:
		
		size_t p = -1;
		do {
			p++;
			word[p] ^= 0x20;			
		} while(word[p] >= 'a');
		
		or:
		
		size_t p = 0;
		do {
			word[p] ^= 0x20;			
		} while(word[p++] >= 'a');
		*/
		
		std::cout << word << std::endl; // Print out new combination
	}
	
	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×