example : if the string is ABC, then all the possible combinations of A, B %26amp; C are :
ABC,ACB,BCA,BAC,CAB,CBA
I need a C language program that prints all the combinations of characters from a string?
Here is something I came up with quickly ( not fully complete, I will leave that to you ! ) 
This quick bit of code will give you a start ( hopefully and no recursion ).
Please note that this is not complete and you will get a memory leak in swap function ( easily fixed btw ! ).
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
void manipulate( char* pString, int index );
char* swap( char* pString, int pos );
char pod[255];
int main(int argc, char* argv[])
{
  char* p = "abc";
  memcpy( pod, p, strlen( p ) );
  for( int i = 0; i %26lt; strlen( p ); i ++ )
  {
    manipulate( pod, strlen( p ) );
  }
  return 0;
}
void manipulate( char* pString, int index )
{
  int i = 1;
  char* p;
  p = pString;   
  while( index %26gt; 1 ) 
  {
    p = swap( p, i );
    index--;
    i++;
    memcpy( pod, p, strlen( pString ) );
    printf( "%s\n", p );
  }
}
char* swap( char* pString, int pos )
{
  char* p = new char[ strlen(pString) + 1 ];
  memset( p, '\0', strlen( pString ) + 1);
  memcpy( p, pString, strlen( pString ) + 1 );
  char temp = p[pos];
  p[pos] = pString[pos-1];
  p[pos-1] = temp;
  return p;
}
Reply:do your homework now, kid.... its for you to learn
if you're still desperate, contact me privately... shall be glad to teach you C
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment