Created
October 27, 2014 14:35
-
-
Save sgade/0a424cdf17010b83b9ac to your computer and use it in GitHub Desktop.
Lettercounter
This file contains 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
// Count the number of letters in any given input | |
#include <stdio.h> | |
#define NUMBERS_IN_THE_ALPHABET 26 | |
#define INPUT_BUFFER_LENGTH 1000 | |
#define OUTPUT_BIG_LETTERS 1 | |
// get an input line | |
size_t getInput(char* buffer, size_t* length) | |
{ | |
printf("Enter text: "); | |
return getline(&buffer, length, stdin); | |
} | |
// main routine | |
int main(void) | |
{ | |
// we only check for alphabet letters | |
int alphabet[NUMBERS_IN_THE_ALPHABET] = { 0 }; | |
// input of the user, max length is defined at the top | |
char input[INPUT_BUFFER_LENGTH]; | |
// typedef unsigned long size_t | |
size_t inputLength = INPUT_BUFFER_LENGTH; | |
inputLength = getInput(input, &inputLength); | |
// count letters | |
for (size_t i = 0; i < inputLength; i++) { | |
char c = input[i]; | |
// increase count | |
if ( c >= 'a' && c <= 'z' ) { | |
alphabet[c - 'a']++; | |
} else if ( c >= 'A' && c <= 'Z' ) { | |
alphabet[c - 'A']++; | |
} else { | |
if ( c == '\n' ) { | |
continue; | |
} | |
printf("Not a character of the alphabet: %c\n", c); | |
} | |
} | |
// output every letter | |
for (unsigned int i = 0; i < NUMBERS_IN_THE_ALPHABET; i++) { | |
char c = ( OUTPUT_BIG_LETTERS ) ? i + 'A' : i + 'a'; | |
printf("%c:\t%d\n", c, alphabet[i]); | |
} | |
// ok | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment