HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Printing out all possible combination of printable ASCII 7-bit characters to stdout

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
combinationbitstdoutallprintablepossibleprintingasciicharactersout

Problem

This takes over 60 minutes to execute. The program is small in size, about 16kb. I am thinking that the repeated calls to printf slows it down significantly, rather than the nested for loops.

I used:


gcc -Wall -o charsprint charsprint.c ./charsprint

Without removing the printf call: How could this source code be smaller and/or faster? Can the nested for loops be condensed using pointers?

``
#include
#include

/*
* print out all combinations of chars between 1 and 5 chars
* 88 keys
* char combinations:
* basic a-f[26+] 0-9[10+] SPACE TAB CAPSLK ,./;'[]\-=
[14+]
SHIFT A-F[26+] <>?:"{}|~!@#$%^&()_+[21+]
* 52+35 = 87 chars available
* 87^5 = 4 984 209 207 possible combinations
*/

char ascii[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f
};

int main(void){
int i,j,x,y,z;
i = j = x = y = z = 0x00;
char result[] = {0x00,0x00,0x00,0x00,0x00,0x00};
for (i=0x20; i<0x7e; i++){
result[0] = ascii[i];
for (j=0x20; j<0x7e; j++){
result[1] = ascii[j];
for (x=0x20; x<0x7e; x++){
result[2] = ascii[x];
for (y=0x20; y<0x7e; y++){
result[3] = ascii[y];
for (z=0x20; z<0x7e; z++){
result[4] =

Solution

It can be smaller if you remove the pointless ascii array, like this:

int main(void){
    int i,j,x,y,z;
    i = j = x = y = z = 0x00;
    char result[] = {0x00,0x00,0x00,0x00,0x00,0x00};
    for (i=0x20; i<0x7e; i++){
        result[0] = i;
        for (j=0x20; j<0x7e; j++){
            result[1] = j;
            for (x=0x20; x<0x7e; x++){
                result[2] = x;
                for (y=0x20; y<0x7e; y++){
                    result[3] = y;
                    for (z=0x20; z<0x7e; z++){
                        result[4] = z;
                        printf("%s\n", result);
                    }
                }
            }
        }
    }
    printf("\nDone...\n");
    return 0;
}


And, it would seem logical that puts(result) should be faster than printf("%s\n", result), though the compiler might optimize both to the same thing anyway.

Code Snippets

int main(void){
    int i,j,x,y,z;
    i = j = x = y = z = 0x00;
    char result[] = {0x00,0x00,0x00,0x00,0x00,0x00};
    for (i=0x20; i<0x7e; i++){
        result[0] = i;
        for (j=0x20; j<0x7e; j++){
            result[1] = j;
            for (x=0x20; x<0x7e; x++){
                result[2] = x;
                for (y=0x20; y<0x7e; y++){
                    result[3] = y;
                    for (z=0x20; z<0x7e; z++){
                        result[4] = z;
                        printf("%s\n", result);
                    }
                }
            }
        }
    }
    printf("\nDone...\n");
    return 0;
}

Context

StackExchange Code Review Q#70816, answer score: 4

Revisions (0)

No revisions yet.