Good day, people! I have here a code in C that needs improvement. It should list all the permutations of ABCDEF where C and E should be beside each other in any order (CE and EC). I know that I must treat C and E as one so I used Z temporarily. But when I run the program, it says that there is an overflow in implicit constant conversion. The only way I know to replace Z with CE and EC is using putc and getc. It's not working so I think there is another code for that. Please guys help me. Thank you in advance! Any kind of help is appreciated.

# include <stdio.h>

FILE * file;

void swap (char *x, char *y) {
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int i, int n) {
   int j;
   if (i == n) {
     printf("%s\n", a);
     fprintf(file, "%s\n", a);
   }
   else {
        for (j = i; j <= n; j++) {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j));
       }
   }
}

int main() {
   char a[] = "ABZDF";
   char ch;
   file = fopen("permutations.txt", "w");
   permute(a, 0, 4);
   while (!feof(file)) {
        ch= getc (file);
        switch(ch) {
            case 'Z': ch='CE';
        }
        putc (ch, file);
    }
   fclose(file);
   getchar();
   return 0;
}

Dani AI

Generated

@Candace Parker The overflow comes from using the multi-character literal 'CE' in a char. In C, 'CE' is not a string; it is an implementation-defined integer that will not fit in a char, and putc() only writes one byte. Also, post-processing the same file you just opened with "w" will not work as intended (you are not reading anything until you close and reopen for reading, and feof() is the wrong loop condition).

A simpler and safer approach is to keep your permutation on 5 symbols using Z as the CE/EC block, and expand Z at the point of output. That avoids any file rewriting and produces exactly the 2 * 5! = 240 permutations you want.

Here is a small helper you can drop in and call twice from your permutation base case (once for CE and once for EC). It writes directly to stdout and your already-open output file:

static void emit_with_pair(const char *perm, FILE *out, char a, char b)
{
    char buf[7]; /* 6 letters + NUL */
    size_t k = 0;
    for (const char *p = perm; *p; ++p) {
        if (*p == 'Z') { buf[k++] = a; buf[k++] = b; }
        else           { buf[k++] = *p; }
    }
    buf[k] = '\0';
    puts(buf);
    fprintf(out, "%s\n", buf);
}

Then, in your permutation base case, replace your current printf/fprintf with:

emit_with_pair(a, file, 'C', 'E');  /* Z -> CE */
emit_with_pair(a, file, 'E', 'C');  /* Z -> EC */

is right that you cannot make putc() output "CE" in one go; write each character or, better yet, print the fully expanded string as shown. If you ever do read from a file for replacement, use int ch; while ((ch = getc(fp)) != EOF) ... and open the file for the correct mode, but for this task direct expansion at output is the cleanest solution.

'CE' is not a character therefor you can't use putc() Fix your switch to output a 'C' then 'E' in case-Z and output the character if not case-Z (default).

Be a part of the DaniWeb community

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