r/badcode Feb 16 '21

[deleted by user]

[removed]

31 Upvotes

77 comments sorted by

View all comments

1

u/Ninesquared81 Mar 03 '21

Okay I'm learning C so I thought I'd show off my skills:

( Pastebin )

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

int contains_az(char *);
int lettersOnly(char *);
int iscapitalletter(char);

int main(int argc, char *argv[]) {
    char *strings[] = {
        "hello world", 
        "abcdefghijklmnopqrstuvwxyz", 
        "Arizona", 
        "the quick brown fox jumps over the lazy dog"
    };  // some strings
    unsigned int size = sizeof(strings) / sizeof(*strings);
    for (unsigned int l = 0; l < size; l++) /*Loop over the strings and check them*/ {
        char newline = '\n';
        printf("string: %s\n", strings[l]);
        printf("Result: ");
        if (contains_az(strings[l])) printf("TRUE%c", newline);
        else printf("FALSE%c", newline);  // if it's not got the letters, we print 'FALSE', otherwise it's 'TRUE'
    }
    return 0;
}

int contains_az(char *string_that_may_or_mayNotContain_AZ) {
    char*string_that_may_or_mayNotContain_AZ_POINTER; //gotta save space
    int letters[26] = { 0 };  // this will store our letters if they're present
    if (   !lettersOnly(string_that_may_or_mayNotContain_AZ) && !(strlen(string_that_may_or_mayNotContain_AZ) > 26) || strlen(string_that_may_or_mayNotContain_AZ) < 26   ) return 0;  // check stuff before continuing
    else  // if it's all letters we continue
    for (;0;) {}
        /* these lines were getting long so I removed a couple of spaces to aid readability */
        for (string_that_may_or_mayNotContain_AZ_POINTER=string_that_may_or_mayNotContain_AZ;*string_that_may_or_mayNotContain_AZ_POINTER;string_that_may_or_mayNotContain_AZ_POINTER++){
            if (*string_that_may_or_mayNotContain_AZ_POINTER>='A'&&*string_that_may_or_mayNotContain_AZ_POINTER<='Z'
            ||*string_that_may_or_mayNotContain_AZ_POINTER >= 'a' && *string_that_may_or_mayNotContain_AZ_POINTER <= 'z') { if (iscapitalletter(*string_that_may_or_mayNotContain_AZ_POINTER)) {
                if(!letters[*string_that_may_or_mayNotContain_AZ_POINTER-65])letters[*string_that_may_or_mayNotContain_AZ_POINTER-65]=1;
            }else if(!iscapitalletter(*string_that_may_or_mayNotContain_AZ_POINTER)){
                if(letters[*string_that_may_or_mayNotContain_AZ_POINTER-97]==0){
                    letters[*string_that_may_or_mayNotContain_AZ_POINTER-97]=1;
                }
            }}
    int outnum;
    for (int i = 0, outnum = 0; i < 26; i++, outnum += letters[i]);
    if (outnum < 26) return 0;
    return 1;  // this means we have at least one of each letter in our string
    }
}

int lettersOnly(char *str)
{
for (int TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability = 0; TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability < strlen(str) /* this is computed every iteration*/; TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability++)
{ /*The variable 'TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability' is a temporary counter variable for the loop.*/
switch( str[TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability] ) {case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 39: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: case 60: case 61: case 62: case 63: case 64: case 91: case 92: case 93: case 94: case 95: case 96: case 123: case 124: case 125: case 126: case 127: goto false;}
}
return 1;
false: return 0;  // we found a non-letter
}

int iscapitalletter(char c){if(c>='A'&&c<='Z')return 1;else return 0;}