#include <iostream>
usingnamespace std;
constint SIZE = 20;
void scramble(char [], int);
unsignedint factorial (int);
unsignedint f = 1;
int main()
{
char ch[SIZE];
cout << "Enter text: ";
cin.getline(ch, SIZE);
int length = 0;
while (ch[length])
length++;
scramble(ch, length);
return 0;
}
void scramble (char arr[], int n)
{
}
unsignedint factorial(int n)//This function is to count the number of possible
{ //combinations.
if (n > 0) //Also any other way to rewrite this without
{ //unsigned int f; as a global function?
f *= n;
f = factorial(n-1);
}
return f;
}
Can someone help me with the scramble function? I'm thinking of a for function that swaps the first character of the array with another one and then in a for loop using recursion like this : scramble(arr + 1, n);
Sample run:
1 2 3 4 5 6 7
Enter text: hey
hey
hye
yeh
yhe
ehy
eyh
//I've got a feeling that my thinking is completely wrong...
Thanks in advance :)
Can someone help me with the scramble function? I'm thinking of a for function that swaps the first character of the array with another one and then in a for loop using recursion like this : scramble(arr + 1, n);
You can do so but you need to decrease n as well: scramble(arr + 1, n - 1);
Check if n > 0 before you're doing anything in your scramble() function
You are looping senselessly here (does not do anything):
1 2
while (ch[length])
length++;
did you mean
1 2 3
while (ch[length])
{ length++;
...}
?
Also your example changes the last character not your first as you mentioned:
hey
hye
while it should be
yhe
Anyway one (simple and not optimized) way to do it (and not use std functions) is to create a same size array and fill it with the correct sequence you wish.