The QBasic Forum     RULES     Other Subforums, Links and Downloads    Index of Threads

 Return to Index  

Let's see if I have this straight

January 16 2007 at 5:29 AM
  (Premier Login iorr5t)
Forum Owner


Response to A program that generates all permutations of its input: anagram.bas

You have written a program in QBasic that accepts an input such as the one below and outputs one like you posted above?

Mac

// anagram.c
//   prints all possible permutations of a word
void permute(int position) ;
void rotate(int position) ;
 unnecessary for QB
#include <stdio.h>         // for printf() etc.
#include <string.h>        // for strlen()
char word[41] ;
int  length ;
void main(void)
{
 these 2 lines combine into 1 INPUT statememnt
 printf("Type word: ") ;
 gets(word) ;      // get word
 length = strlen(word) ;
 printf("%s\n", word) ;    // print result
 permute(0) ;      // call permute
 0 changed to 1 because of string offset differences between c and QB
}
  // permute
  // prints all permutations of a word
  void permute(int startperm)
  {
   int j ;
   if (length-startperm < 2)     // exit if one character
   had to change 2 to 1 because of string offset differences between
   c and QB
       return ;
   for (j = startperm ; j < length - 1 ; j++)  // # chars in word -1
     the { and } delimit the body of the loop.  the first 2 lines are
     pretty much a straght port from c to QB, just had to remove the ;s
     and change the comment delimiters
     {
       permute(startperm + 1) ;    // permute all but first
       rotate(startperm) ;         // rotate all chars
       printf("%s\n", word) ;      // print result
     c's print function is a bit different.  it was written to put each word
     on its own line, but i added the ',' so the words would (hopefully)
     stay on the screen
     }
   again, a relatively straight port from c to QB
   permute(startperm + 1) ;      // restore word to
   rotate(startperm) ;           //   original form
  }
  // rotate(startrot)
  // rotates word one character left
  // word begins at character position startrot
  void rotate(int startrot)
  {
   int j ;
   char ch ;
   ch = word[startrot] ;               // save first character
   for (j = startrot ; j < length - 1 ; j++) // move other chars left
     word[j] = word[j+1] ;             //  one position
   word[length - 1] = ch ;             // move first char to last
   this function could be written much more simply in QB because of the
   richer string-handling functions.  c's string handling is much more
   primitive.  the line below does everything the 4 lines from the c prog
   did
  }

 
 Respond to this message