Hi people I'm programming in C like a month. I just made this code yesterday, but I want the same result with a better code without using recursion for optimization, i think mine is sloppy. Also im using linux distribution... Any ideas to change this code in 1 or 2 functions?

Anything will help! Thanks.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define Plain "plain.txt" 


int length ;
int i;
char alphalower[] = "abcdefghijklmnopqrstuvwxyz"; 


int main(){

	printf("\tEnter Maximum Char Length: ");
	scanf("%d",&length);
	for(i=1;i<=length;i++){
		combinations(alphalower,i);
	}
   return 0;

}

int possibles (const void * a, const void * b)
{
	return ( *(char *)a - *(char *)b );
}
void back2 (char *str, char* info, int last, int index)
{
	FILE *fp;
	if ((fp = fopen(Plain, "a+")) == NULL)
	{
		perror (Plain);
		exit (EXIT_FAILURE);
	}
	int i, length = strlen(str);
	for ( i=0; i < length; i++ )
	{
		info[index] = str[i] ;
		if (index == last)
			fprintf(fp,"%s\n", info);
		else 
			back2 (str, info, last, index + 1);
	}
	fclose(fp); 

}

int combinations(char *str, int length)
{

	char *info = (char *) malloc (sizeof(char) * (length + 1)) ;
	info[length] = '\0';
	qsort(str, length, sizeof(char), possibles);
	back2 (str, info, length-1, 0);
	free(info);

	return 0;
}

Edited 4 Years Ago by Sundown G: n/a

could you explain what your program is supposed to do? some comments on particular lines would be nice

...just a note, always have a return 0; statement at the end of int main to properly stop the execution, it might work without it on linux but the code should be able to be compatible on all compilers

Edited 4 Years Ago by zeroliken: n/a

Thanks for the response. Well my program allow to user check for possibles combinations on the alphabet.. and save on file !

If the user enter 1 as max len ... the output will be all possibles combinations of the alphabet with len 1 , if enter 2 ....

I did the program with for loop ( but i want it dynamic) if user enter 120 .. all possible combinations.

example: 1 (26 possibles combinations)

a
b
c
d
e
f
.
.
.
.
z

Example:
if user enter :: 2 ( 702 possible combinations)

output will be

a
b
c
d
e
.
..
z
aa
ab
ac
ad
..
.
.
.
zz

my code: recursion with dynamic memory

Edited 4 Years Ago by Sundown G: n/a

I will have to think about if I could do it without recursion, but here is a cleaned up version with recursion.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define Plain       "plain.txt"
#define MAX_LENGTH  100

void doIt(FILE *fp, char *str, char *alpha, int numLength){
  int i;
  if ( --numLength >= 0 ){
     for (i=0; i<strlen(alpha);i++){
        sprintf(str,"%s%c",str,alpha[i]);
        doIt(fp,str,alpha,numLength);
        str[strlen(str)-1] = '\0';
     }
  }
  else {
     fprintf(fp,"%s\n",str);
  }
  return;
}

int main(){
   char alphalower[] = "abcdefghijklmnopqrstuvwxyz";
   char str[MAX_LENGTH] = {'\0'};
   FILE *fp;
   int length,i;
   printf("\tEnter Maximum Char Length: ");
   scanf("%d",&length);
   fp = fopen(Plain, "w");
   if ( length <= MAX_LENGTH && fp != NULL){
      for (i=1; i<=length; i++){
         doIt(fp,str,alphalower,i);
      }
      fclose(fp);
   }
   else {
     if ( fp == NULL )
        printf("Unable to open file %s -> %s\n",Plain,strerror(errno));
     else
        printf("Error:  Maximum Char length must be <= %d\n",MAX_LENGTH);
   }
   return 0;
}
Comments
thanks again

I will have to think about if I could do it without recursion, but here is a cleaned up version with recursion.

Thanks to solve anyways! +Rep. I think is the better way -- recursion!

Also were you learned C programming..books, tuts, class? i need some good references!

If you or anyone interested on programming contest! check the link below!

contest.codequest.spotify.com

This the reason Im practice C

Greetings.

This question has already been answered. Start a new discussion instead.