Beginners C Programming Challenge

This is a discussion on Beginners C Programming Challenge within the C Programming forums, part of the General Programming Boards category; Code: /* ** PERMUTE.C - prints all permutations of an input string ** ** adapted from public domain demo by ...

  1. #16
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Source Code: String Permutations

    Code:
    /*
    **  PERMUTE.C - prints all permutations of an input string
    **
    **  adapted from public domain demo by Jon Guthrie for J. Uslander (4/1/2008).
    **
    */
    
    #include    <string.h>
    #include    <stdlib.h>
    #include    <stdio.h>
    
    int     charcmp(char *, char *);
    
    void    permute(char *, int, int);
    
    char string[1024];
    
    void clrscr(void);
    
    int     main(void)
    {
        clrscr();
            printf("Enter a single word string, using only letters and no spaces.\n");
        fgets(string, strlen(string), stdin);
        int length = strlen(string);
                if (length != 0 )
          {      /* It only works if they're printed in order */
    
          qsort(string, length, 1, (int(*)(const void *, const void *))charcmp);
    
          permute(string, 0, length);
    
          return 0;
          }
          else {
              printf("This program requires user to input a single word string(non-NULL).\n");
              abort();
    }
    }
    
    /*
    **  This function prints all of the permutations of string "array"
    **  (which has length "len") starting at "start."
    */
    
    void    permute(char *array, int start, int len)
    {
          int j;
          char    *s;
    
          if(start < len)
          {
                if(NULL == (s = malloc(len + 1)))	/* Bug fixed by Stephan Wilms	*/
                {
                      printf("\n\nMemory error!!!\a\a\n");
                      abort();
                }
    
                strcpy(s, array);
                for(j=start ; j<len ; ++j)
                {
                      int     temp;
    
                      if((j == start) || (s[j] != s[start]))
                      {     /* For each character that's different    */
                            /* Swap the next first character with...  */
                            /* the current first                      */
                            temp = s[j];
                            s[j] = s[start];
                            s[start] = temp;
                            permute(s, start+1, len);
                      }
                }
                free(s);
          }
          else  puts(array);
    }
    
    
    
    int charcmp(char *a, char *b)
    {
          return(*a - *b);
    }
    
    void clrscr(void)
    {
        char a[80];
        printf("\033[2J");     /* clear the entire screen*/
        printf("\033[0;0f");     /* move cursor to the top left hand corner */
    }
    Thanks.
    UC

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       fgets(string, sizeof(string), stdin);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Defining variables in the "middle" of your code is not standard and can lead to unexpected behavior. And Dave's suggestion seems to have cured your runtime error for me. I can't say it's giving me intelligible output, but it is at least giving me output.

  4. #19
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Here you go, champ. Notice my changes in red.

    Code:
    /*
     **  PERMUTE.C - prints all permutations of an input string
     **
     **  adapted from public domain demo by Jon Guthrie for J. Uslander (4/1/2008).
     **
     */
    
    #include    <string.h>
    #include    <stdlib.h>
    #include    <stdio.h>
    
    int charcmp(char *, char *);
    void permute(char *, int, int);
    void clrscr(void);
    
    int main(void)
    {
    	char string[1024];
    	int length;
    	char throwaway;
    	
    	printf("Enter a single word string, using only letters and no spaces: ");
    	fflush(stdout);
    	fgets(string, sizeof(string), stdin);
    	
              	length = strlen(string);
    
    	if (length != 0 )
    	{      /* It only works if they're printed in order */
    
    		qsort(string, length, 1, (int(*)(const void *, const void *))charcmp);
    
    		permute(string, 0, length);
    
                                    printf("This program requires user to input a single word string(non-NULL).\n");
    		printf("Press ENTER to exit.\n");
    		fflush(stdout);
    		scanf("&#37;c", &throwaway);
    
    		return 0;
    	}
    	else {
    		printf("This program requires user to input a single word string(non-NULL).\n");
    		printf("Press ENTER to exit.\n");
    		scanf("%c", &throwaway);
    	}
    	
    	return 0;
    }
    
    /*
     **  This function prints all of the permutations of string "array"
     **  (which has length "len") starting at "start."
     */
    
    void permute(char *array, int start, int len)
    {
    	int j;
    	char    *s;
    
    	if(start < len)
    	{
    		if(NULL == (s = malloc(len + 1)))	/* Bug fixed by Stephan Wilms	*/
    		{
    			printf("\n\nMemory error!!!\a\a\n");
    			abort();
    		}
    
    		strcpy(s, array);
    		for(j=start ; j<len ; ++j)
    		{
    			int     temp;
    
    			if((j == start) || (s[j] != s[start]))
    			{     /* For each character that's different    */
    				/* Swap the next first character with...  */
    				/* the current first                      */
    				temp = s[j];
    				s[j] = s[start];
    				s[start] = temp;
    				permute(s, start+1, len);
    			}
    		}
    		free(s);
    	}
    	else  
                     {
                                printf("%s", array);
                     }
    }
    
    
    
    int charcmp(char *a, char *b)
    {
    	return(*a - *b);
    }
    
    void clrscr(void)
    {
    	//char a[80];
    	printf("\033[2J");     /* clear the entire screen*/
    	printf("\033[0;0f");     /* move cursor to the top left hand corner */
    }
    Last edited by JDGATX; 04-01-2008 at 02:40 PM.

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    The Champ returns.

    Thanks. I used all your suggestions and the output is cleaner, but still includes partial words, single letters and words larger than string length. How can I clean this up. Also the change you made to clrscr function //. What does that do?

    //char a[80];
    Thanks for all your help.
    Champ
    Last edited by UCnLA; 04-01-2008 at 03:24 PM. Reason: include source code

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Don't include the newline in the string.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #22
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by UCnLA View Post
    Thanks. I used all your suggestions and the output is cleaner, but still includes partial words, single letters and words larger than string length. How can I clean this up. Also the change you made to clrscr function //. What does that do?



    Thanks for all your help.
    Champ

    You never used that variable in your function, so I commented it out. To be honest, your clrscr() function doesn't do a whole lot besides muck things up for me. I would suggest removing it completely.

  8. #23
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Quote Originally Posted by Dave_Sinkula View Post
    Don't include the newline in the string.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    A)I implemented your suggestion, but I also get strings less than length of original string. How can I eliminate these? I thought the following would work, but I still get the dimminished strings.
    Also, do I have to memorize all these little tricks, because I never would think of them on my own.

    A)
    Code:
    if((p=strchr(array,'\n'))!= NULL && sizeof(array)==len)     // Test for and remove newline character. And prints arrays of size length only.
               { *p= '\0';
              printf("&#37;s\n", array);
              }
    Thanks UC
    Last edited by UCnLA; 04-01-2008 at 06:35 PM. Reason: Tweaked code a bit

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       printf("Enter a single word string, using only letters and no spaces: ");
       fflush(stdout);
       if ( fgets(string, sizeof(string), stdin) )
       {
          char *newline = strchr(string, '\n');
          if ( newline )
          {
             *newline = '\0';
          }
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 2
    Last Post: 03-18-2008, 12:15 PM
  2. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 05:56 AM
  3. for beginner's or anyone interested
    By Iconoklast in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-25-2004, 01:45 PM
  4. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21