string decoder

hey all!

I am writing a program that takes a string, and tries to create the copy of it using all possible combinations, like a sort of bruteforce.
Can someone help me? this is what i have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
   string a; 
   getline(cin,a);
   char b[a.length()];
   for(int l = 0;l<a.length();l++){
           b[l]='a';
           }
   int num = 0;
    for(int i = 0;i<a.length();i++){
            for(int z = b[i];z<300;z++){
                    b[i]=z;
                    
                    cout<<b<<endl;
                    
                    }
            if(a==b){
                     cout<<"works";

                     }
            }
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


This doesnt work. I am trying to decode using the ansi table, witch every character of the array b starts at a, and add to it until it is 300.
Can someone point out what i am doing wrong?
Thanks in advance!
someone?
We were given this problem before when I join a small programming competition before but I already lost the source code.

What you're trying to do here is string permutation. After a quick google search..
I get this from the first search result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This program finds permutations using a recursive method
// modified Dev <strong class="highlight">C++</strong> from a wonderful article at:
// http://www.codeproject.com/cpp/cppperm1.asp

#include<iostream>
#include<cstring>

using namespace std;

void char_permutation(char str[],char append[])
{
  int length = strlen(str);
  if (length)
  {
    for(int i=0;i<length;++i)
    {
      char* str1 = new char[length+1];
      int cnt;
      int cnt2;
      for(cnt=0,cnt2=0; cnt<length; ++cnt,++cnt2)
      {
        if (cnt == i)
        {
          str1[cnt] = str[++cnt2];
          continue;
        }
        else
          str1[cnt] = str[cnt2];
      }
      str1[cnt] = '\0';

      int alength = strlen(append);
      char* append1 = new char [alength+2];
      strncpy(append1,append,alength);
      append1[alength] = str[i];
      append1[alength+1] = '\0';

      char_permutation(str1,append1);

      delete []str1;
      delete []append1;
    }
  }
  else
  {
    cout << append << endl;
  }
}


int main()
{
  char str[] = "BUSH";  // shows a little humor
  char append[] = "\0";

  cout << "Original = " << str << endl;
  char_permutation(str,append);
	cout << "Done ........" << endl;

	cin.get();  // wait
  return 0;
}
Original = BUSH
BUSH
BUHS
BSUH
BSHU
BHUS
BHSU
UBSH
UBHS
USBH
USHB
UHBS
UHSB
SBUH
SBHU
SUBH
SUHB
SHBU
SHUB
HBUS
HBSU
HUBS
HUSB
HSBU
HSUB
Done ........

so next time remember to search before you post
no this is not what i want.
I want the program to pick a string, lets say bbbb.
the program should go like this:
aaaa
aaab
aaac...
.....
azzz
baaa
baab
baac...
...
bbba
bbbb
and then print out the number of guesses it took to get the string
Are we given the length of the string or only the string?
currently i am doing with length
This isn't exactly what you need but may help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char char_min='!',char_max='~';

int recursion(char *&str,int length)
{
  for(int i=char_min;i!=char_max;i++)
    {
      str[length]=i;
      cout <<str<<endl;
      if(length>0)recursion(str,length-1);
    }
return 0;
}



int main()
{
char *str=strdup("aaa");
recursion(str,strlen(str)-1);

  return 0;
}

I wrote this just now so there might be some errors, also is only supports lower case I guess. But it should be enough to get you started.

Hope it helps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

int main()
{
	std::string key = "abbb";
	std::string tmp = "aaaa";

	int pos = tmp.length()-1;

	while( true )
	{
		if( key==tmp ) {
			std::cout << tmp << "\n";
			break;
		}

		if( tmp[pos]<'z' ) {
			tmp[pos]++;
			std::cout << tmp << "\n";
			if( pos<tmp.length()-1 ) pos = tmp.length()-1;
		}
		else if( tmp[pos]=='z' ) {
			tmp[pos] = 'a';
			pos--;
		}
	}
}
Thank you very much!
One more question, could you explain the logic behind these two statements?
1
2
3
4
5
6
7
8
9
if( tmp[pos]<'z' ) {
			tmp[pos]++;
			cout << tmp << "\n";
			if( pos<tmp.length()-1 ) pos = tmp.length()-1;
		}
		else if( tmp[pos]=='z' ) {
			tmp[pos] = 'a';
			pos--;
		}

And if you can, could you explain how to write the same code again, but with different checking without knowing the length of the word, for example
a
b
...
z
aa
ab
...
zz
aaa
aab
...
zzz

and so on

I also added a wider range of characters to it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;
int main()
{
	string key = "asddd";
	string tmp = "####";

	int pos = tmp.length()-1;

	while( true )
	{
		if( key==tmp ) {
			cout << tmp << "\n";
			break;
		}

		if( tmp[pos]<'z' ) {
			tmp[pos]++;
			cout << tmp << "\n";
			if( pos<tmp.length()-1 ) pos = tmp.length()-1;
		}
		else if( tmp[pos]=='z' ) {
			tmp[pos] = '#';
			pos--;
		}
	}
	system("pause");
}

Now just to get rid of the tmp string length...
Last edited on
I hope I did well explaining this, and please excuse my english.

1
2
3
4
5
6
7
8
9
10
11
12
if( tmp[pos]<'z' ) { //1. if the last char is a-y then increament by 1
	tmp[pos]++;
	std::cout << tmp << "\n"; //2. then print
	if( pos<tmp.length()-1 ) pos = tmp.length()-1; //5. alrighty the position
                                                        //at the back is already 
							//increamented, let's go home
							//it is now aab[a]
}
else if( tmp[pos]=='z' ) { //3. we can't increament z 
	tmp[pos] = 'a';        //   so go back to a
	pos--; //4. move back, for example aaa[z] moves to aa[a]a
}
Last edited on
Thank you! thanks so much now i understand everything!
could you explain how to write the same code again, but with different checking without knowing the length of the word
Sorry but no, you should do it on your own. Though you can still ask for help when you get errors.


Thank you! thanks so much now i understand everything!
I'm glad it help you to get started.
Topic archived. No new replies allowed.