Hi,

I got lost with some prj, hope you can give a hand.

I need to create a tiny code, that generating between several variables,
with loop that generate all char variations in new row.
The order of all variables need to remain as it is,
and to take effect in 6 chars minimum situation ..

For example:

var1: adir
var2: going
var3: forward

The output should be like that:

adirgo
adirgoi
adirgoin
adirgoing
adirgoingf
adirgoingfo
adirgoingfor
adirgoingforw
adirgoingforwa
adirgoingforwar
adirgoingforward

goingf
goingfo
goingfor

and so on...

Thanks in advance
Adir

Recommended Answers

Have a go at it, and then show us what you tried.

David

Jump to Post

All 3 Replies

Have a go at it, and then show us what you tried.

David

Unfortunately, we can't help you much until you show us what you've tried first.

This looks like a good candidate for nested for-loops.

Have a go at it, and then show us what you tried.

David

Hi again, and thanks for quick reply..
I worked on this code, and its not mine ofcourse ..

#include<iostream>
#include<cstring>
#include <fstream>


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[] = "adirgoingforward";  // shows a little humor
  char append[] = "\0";  

  cout << "Original = " << str << endl;
  char_permutation(str,append);
  cout << "Done ........" << endl;
	
	cin.get();  // wait
  return 0;
}

huge regards!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.