Comments: Flat Nested Threaded Embedded Oldest First Newest First Write a program that takes input a char array and outputs all the combinations of the characters in By Abraham on Thursday, May 29, 2008 (UMST) /*Write a program that takes input a char array and outputs all the combinations of the characters in the character array. Example: consier char array 'a','b','c'} the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'. */ /* AG: concatinate string abc to abcabcthe parse till len of given stringabcbcacabnow start from reverse (len*2) - 1 len timescbabccacb*/ #include <stdio.h>#include <string.h> main(){ char a[3] = {'a','b','c'}; char buffer[100] = {'\0'}; int len = sizeof(a)/sizeof(char); int i =0, j = 0; memset (buffer,'\0',100); for (i = 0 ;i < len ;i++) { buffer[i] = a[i]; } strcat(buffer,buffer); printf ("buffer = %s\n",buffer); for (i = 0 ;i<len;i++) { for (j =0;j<len;j++) { printf ("%c",buffer[i+j]); } printf ("\n"); } for (i = (len*2) -1 ;i>= len ;i--) { for (j =0;j<len;j++) { printf ("%c",buffer[i-j]); } printf ("\n"); }} /* Output:buffer = abcabcabcbcacabcbabacacbPress any key to continue*/ Reply to this Comment Average Rating: doesn't work for more values By ksumit on Saturday, June 14, 2008 (UMST) Though this does work for {a,b,c}; it fails miserably for n>3 Reply to this Comment Solution in C# (Works for n>3 also) By kp.zeus on Saturday, July 05, 2008 (UMST) public static string[] GetCombinations(char[] inputs) { if (inputs == null) { throw new ArgumentNullException("inputs"); } string[] combinations = new string[inputs.Length * (inputs.Length - 1)]; int i = 0, j = 0, pos = 0, resultCount = 0; char[] temp = new char[inputs.Length]; while (resultCount < combinations.Length) { while (i < inputs.Length - 2 && resultCount < combinations.Length) { pos = 0; //Forward while (pos <= i) { temp[pos] = inputs[pos]; pos++; } j = pos; while (j < inputs.Length) { temp[j] = inputs[j]; j++; } combinations[resultCount] = new string(temp); resultCount++; //Reverse j = inputs.Length - 1; while (pos < inputs.Length) { temp[pos] = inputs[j]; j--; pos++; } i++; combinations[resultCount] = new string(temp); resultCount++; } i = 0; char start = inputs[0]; //Shift while (i < (inputs.Length - 1)) { inputs[i] = inputs[i + 1]; i++; } inputs[inputs.Length - 1] = start; i = 0; } return combinations; } Reply to this Comment c# version. ok with long inputs By factorme on Thursday, September 04, 2008 (UMST) internal class Program { private static void Main(string[] args) { string input = Console.ReadLine(); Console.WriteLine("---------result----------"); PrintAllCombinations(input); Console.WriteLine("---------end----------"); Console.ReadLine(); } /// <summary> /// Print all possible combinations of the chars inside a string. /// </summary> /// <remarks> /// Algorithom: /// 1. Simulate the input with a number 1234...n where n is the input's length /// 2. Increase the number by 1 until the result exceeds n....321 /// 3. Generate an result string based on each calculation result of the steps looped in 2. /// 2. Use an int array to simulate big numbers in case the input string is very long. /// </remarks> /// <param name="input"></param> private static void PrintAllCombinations(string input) { if (string.IsNullOrEmpty(input) || input.Length == 1) { Console.WriteLine(input); return; } int len = input.Length; int[] indice = new int[len]; for (int i = 0; i < len; i++) { indice[i] = i; } bool goon = true; int toAdd = 0; while (goon) { Print(input, indice); int x = indice[len - 1]; x += 1 + toAdd; if (x > len) { indice[len - 1] = 0; for (int i = len - 2; i >= 0; i--) { int temp = indice[i]; temp++; if (temp > len) { if (i == 0) { goon = false; } indice[i] = 0; } else { indice[i] = temp; break; } } } else { indice[len - 1] = x; } } } private static void Print(string input, params int[] indice) { for (int i = 0; i < input.Length; i++) { if (Array.IndexOf(indice, i) == -1) { return; } } string temp = string.Empty; foreach (int i in indice ?? new int[0]) { temp += input[i]; } Console.WriteLine(_count + ":\t" + temp); _count++; } private static int _count = 1; } Reply to this Comment This is very simple By aravind on Thursday, October 09, 2008 (UMST) //If the string is in letter and len is the length string letter; for (i=0;i<n;i++) for(j=0;j<n;j++) { temp=letter[i]; letter[i+1]=letter[i]; letter[i+1]=temp; cout<< letter; } Reply to this Comment Simple answer By aravind on Thursday, October 09, 2008 (UMST) Sorry the above post had a mistake and i dont see option to delete //If the string is in letter and len is the length string letter; for (i=0;i<n;i++) for(j=0;j<n;j++) { temp=letter[j]; letter[j+1]=letter[j]; letter[j+1]=temp; cout<< letter; } Reply to this Comment Program in Java By yogi_1306 on Saturday, October 11, 2008 (UMST) public class Permutation { public static void main(String[] args){ permute("abcd"); } public static void permute(String letters) { permute(letters, 0); } private static String swap(String letters, int i, int j){ char[] asArray = letters.toCharArray(); char tmp = asArray[i]; asArray[i] = asArray[j]; asArray[j] = tmp; return new String(asArray); } private static void permute(String letters, int k) { if (k == letters.length()-1) { System.out.println(letters); return; } for (int idx = k; idx < letters.length(); idx++){ letters = swap(letters, k, idx); permute(letters, k+1); } } } Reply to this Comment
/*Write a program that takes input a char array and outputs all the combinations of the characters in the character array. Example: consier char array 'a','b','c'} the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'. */
/* AG: concatinate string abc to abcabcthe parse till len of given stringabcbcacabnow start from reverse (len*2) - 1 len timescbabccacb*/
#include <stdio.h>#include <string.h>
main(){ char a[3] = {'a','b','c'}; char buffer[100] = {'\0'}; int len = sizeof(a)/sizeof(char); int i =0, j = 0; memset (buffer,'\0',100); for (i = 0 ;i < len ;i++) { buffer[i] = a[i]; } strcat(buffer,buffer); printf ("buffer = %s\n",buffer);
for (i = 0 ;i<len;i++) { for (j =0;j<len;j++) { printf ("%c",buffer[i+j]); } printf ("\n"); }
for (i = (len*2) -1 ;i>= len ;i--) { for (j =0;j<len;j++) { printf ("%c",buffer[i-j]); } printf ("\n"); }}
/*
Output:buffer = abcabcabcbcacabcbabacacbPress any key to continue*/
Reply to this Comment
internal class Program { private static void Main(string[] args) { string input = Console.ReadLine(); Console.WriteLine("---------result----------"); PrintAllCombinations(input); Console.WriteLine("---------end----------"); Console.ReadLine(); }
/// <summary> /// Print all possible combinations of the chars inside a string. /// </summary> /// <remarks> /// Algorithom: /// 1. Simulate the input with a number 1234...n where n is the input's length /// 2. Increase the number by 1 until the result exceeds n....321 /// 3. Generate an result string based on each calculation result of the steps looped in 2. /// 2. Use an int array to simulate big numbers in case the input string is very long. /// </remarks> /// <param name="input"></param> private static void PrintAllCombinations(string input) { if (string.IsNullOrEmpty(input) || input.Length == 1) { Console.WriteLine(input); return; }
int len = input.Length; int[] indice = new int[len]; for (int i = 0; i < len; i++) { indice[i] = i; }
bool goon = true; int toAdd = 0; while (goon) { Print(input, indice); int x = indice[len - 1]; x += 1 + toAdd; if (x > len) { indice[len - 1] = 0; for (int i = len - 2; i >= 0; i--) { int temp = indice[i]; temp++; if (temp > len) { if (i == 0) { goon = false; }
indice[i] = 0; } else { indice[i] = temp; break; } } } else { indice[len - 1] = x; } } }
private static void Print(string input, params int[] indice) { for (int i = 0; i < input.Length; i++) { if (Array.IndexOf(indice, i) == -1) { return; } }
string temp = string.Empty; foreach (int i in indice ?? new int[0]) { temp += input[i]; } Console.WriteLine(_count + ":\t" + temp); _count++; }
private static int _count = 1; }
//If the string is in letter and len is the length
string letter;
for (i=0;i<n;i++)
for(j=0;j<n;j++)
{
temp=letter[i];
letter[i+1]=letter[i];
letter[i+1]=temp;
cout<< letter;
}
Sorry the above post had a mistake and i dont see option to delete
temp=letter[j];
letter[j+1]=letter[j];
letter[j+1]=temp;