Guest User

Bad Code Coding Challenge #56 Beginner C code

a guest
Mar 3rd, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int contains_az(char *);
  5. int lettersOnly(char *);
  6. int iscapitalletter(char);
  7.  
  8. int main(int argc, char *argv[]) {
  9.     char *strings[] = {
  10.         "hello world",
  11.         "abcdefghijklmnopqrstuvwxyz",
  12.         "Arizona",
  13.         "the quick brown fox jumps over the lazy dog"
  14.     };  // some strings
  15.     unsigned int size = sizeof(strings) / sizeof(*strings);
  16.     for (unsigned int l = 0; l < size; l++) /*Loop over the strings and check them*/ {
  17.         char newline = '\n';
  18.         printf("string: %s\n", strings[l]);
  19.         printf("Result: ");
  20.         if (contains_az(strings[l])) printf("TRUE%c", newline);
  21.         else printf("FALSE%c", newline);  // if it's not got the letters, we print 'FALSE', otherwise it's 'TRUE'
  22.     }
  23.     return 0;
  24. }
  25.  
  26. int contains_az(char *string_that_may_or_mayNotContain_AZ) {
  27.     char*string_that_may_or_mayNotContain_AZ_POINTER; //gotta save space
  28.     int letters[26] = { 0 };  // this will store our letters if they're present
  29.     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
  30.     else  // if it's all letters we continue
  31.     for (;0;) {}
  32.         /* these lines were getting long so I removed a couple of spaces to aid readability */
  33.         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++){
  34.             if (*string_that_may_or_mayNotContain_AZ_POINTER>='A'&&*string_that_may_or_mayNotContain_AZ_POINTER<='Z'
  35.             ||*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)) {
  36.                 if(!letters[*string_that_may_or_mayNotContain_AZ_POINTER-65])letters[*string_that_may_or_mayNotContain_AZ_POINTER-65]=1;
  37.             }else if(!iscapitalletter(*string_that_may_or_mayNotContain_AZ_POINTER)){
  38.                 if(letters[*string_that_may_or_mayNotContain_AZ_POINTER-97]==0){
  39.                     letters[*string_that_may_or_mayNotContain_AZ_POINTER-97]=1;
  40.                 }
  41.             }}
  42.     int outnum;
  43.     for (int i = 0, outnum = 0; i < 26; i++, outnum += letters[i]);
  44.     if (outnum < 26) return 0;
  45.     return 1;  // this means we have at least one of each letter in our string
  46.     }
  47. }
  48.  
  49. int lettersOnly(char *str)
  50. {
  51. for (int TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability = 0; TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability < strlen(str) /* this is computed every iteration*/; TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability++)
  52. { /*The variable 'TemporaryCounterVariableWithNameThatIsNeededToConveyUsageAndAvoidAmbiguityToImproveReadability' is a temporary counter variable for the loop.*/
  53. 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;}
  54. }
  55. return 1;
  56. false: return 0;  // we found a non-letter
  57. }
  58.  
  59. int iscapitalletter(char c){if(c>='A'&&c<='Z')return 1;else return 0;}
Advertisement
Add Comment
Please, Sign In to add comment