Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4.  
  5. bool isUpper(int);
  6. bool isLower(int);
  7. bool isSubstring(char*, char*);
  8. int toUpper(int);
  9. void removeNewLines(char*);
  10.  
  11. int main(int argc, char *argv[]){
  12.     (void)argc;
  13.  
  14.     char    city[100];
  15.     char    cities[42][100];
  16.     char    possibleChars[100];
  17.     int     possibleCharsIndex = 0;
  18.     int     i = 0;
  19.     int     state = 0;
  20.  
  21.     for (unsigned int i = 0; i < strlen(argv[1]); i++) {
  22.         argv[1][i] = toUpper(argv[1][i]);
  23.     }
  24.  
  25.     while (fgets(city, 100, stdin) != NULL) {
  26.         for (unsigned int i = 0; i < strlen(city); i++) {
  27.             city[i] = toUpper(city[i]);
  28.         }
  29.         removeNewLines(city);
  30.         strcpy(cities[i++], city);
  31.     }
  32.  
  33.     for (unsigned int i = 0; i < 42; i++) {
  34.         if(strcmp(argv[1], cities[i]) == 0){
  35.             printf("FOUND: %s\n", cities[i]);
  36.             state = 1;
  37.         }
  38.     }
  39.  
  40.     if (state == 0) {
  41.         for (unsigned int i = 0; i < 42; i++) {
  42.             if(isSubstring(argv[1], cities[i])){
  43.                 possibleChars[possibleCharsIndex] = cities[i][strlen(argv[1])];
  44.                 possibleCharsIndex++;
  45.                 state = 2;
  46.             }
  47.         }
  48.     }
  49.  
  50.     if (state == 2) {
  51.         printf("ENABLE: ");
  52.         for (unsigned int i = 0; i < 50; i++) {
  53.             printf("%d: %c\n", i, possibleChars[i]);
  54.         }
  55.         printf("\n");
  56.     } else if(state == 0){
  57.         printf("NOT FOUND\n");
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
  63. bool isUpper(int c){
  64.     return c >= 'A' && c <= 'Z';
  65. }
  66.  
  67. bool isLower(int c){
  68.     return c >= 'a' && c <= 'z';
  69. }
  70.  
  71. int toUpper(int c){
  72.     if(isUpper(c)){
  73.         return c;
  74.     } else if(isLower(c)){
  75.         return c - 'a' + 'A';
  76.     } else {
  77.         return c;
  78.     }
  79. }
  80.  
  81. bool isSubstring(char *sub, char *base){
  82.     if (strlen(sub) > strlen(base)) {
  83.         return false;
  84.     } else {
  85.         bool isSubstring = true;
  86.         for (unsigned int i = 0; i < strlen(sub); i++) {
  87.             if(sub[i] != base[i]) isSubstring = false;
  88.         }
  89.         return isSubstring;
  90.     }
  91. }
  92.  
  93. void removeNewLines(char *word){
  94.     for (unsigned int i = 0; i < strlen(word); i++) {
  95.         if(word[i] == 10){
  96.             word[i] = 0;
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement