Last active
November 25, 2015 21:47
-
-
Save rmccullagh/7137db976c01493f46fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * Copyright (c) 2015 Ryan McCullagh <me@ryanmccullagh.com> | |
| * | |
| * This program is free software: you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation, either version 3 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * This program is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| */ | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <ctype.h> | |
| int main(void) | |
| { | |
| char c; | |
| char buffer[1024]; | |
| size_t i = 0; | |
| typedef struct my_char { | |
| int n; // ASCII UPPER CASE 65:65+26 | |
| int count; | |
| } my_char; | |
| my_char alphabet[65 + 26]; | |
| int b; | |
| for(b = 0; b < (65 + 26); b++) { | |
| my_char item = {0, 0}; | |
| alphabet[b] = item; | |
| } | |
| /* | |
| * check if char is between alphabet range | |
| * conver to int, subtract 1, increment at index converted to int | |
| * loop through all to check that if any index value is zero, we failed, else OK | |
| */ | |
| while((c = fgetc(stdin)) != -1) { | |
| /* end of line, or c would be equal to the length */ | |
| /* can't push anymore, because sizeof(buffer) - 1 is for '\0' */ | |
| if(c == '\n' || i == sizeof(buffer) - 1) { | |
| buffer[i] = '\0'; | |
| size_t len = strlen(buffer); | |
| size_t j; | |
| for(j = 0; j < len; j++) { | |
| char cc = buffer[j]; | |
| if((cc <= 'Z' && cc >= 'A') || (cc <= 'z' && cc >= 'a')) { | |
| int ccc = toupper((int)cc); | |
| if(alphabet[ccc - 1].count) { | |
| my_char item = {ccc, ++alphabet[ccc-1].count}; | |
| alphabet[ccc - 1] = item; | |
| } else { | |
| my_char item = {ccc, 1}; | |
| alphabet[ccc - 1] = item; | |
| } | |
| } | |
| } | |
| int a; | |
| int failed = 0; | |
| for(a = 64; a < (65+26)-1; a++) { | |
| if(!alphabet[a].n) { | |
| failed = 1; | |
| break; | |
| printf("%c:%d\n", (char)alphabet[a].n, alphabet[a].count); | |
| } | |
| } | |
| if(failed) | |
| printf("Not a valid input\n"); | |
| else | |
| printf("This is a valid input\n"); | |
| memset(buffer, '\0', sizeof(buffer)); | |
| i = 0; | |
| for(a = 0; a < (65+26); a++) { | |
| my_char new_item = {0, 0}; | |
| alphabet[a] = new_item; | |
| } | |
| } else { | |
| buffer[i++] = c; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment