Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created October 31, 2010 20:02
Show Gist options
  • Save Cellane/657061 to your computer and use it in GitHub Desktop.
Save Cellane/657061 to your computer and use it in GitHub Desktop.
//
// charStats.c
// Zkouska
//
// Created by Milan Vít on 31. 10. 10.
// Copyright (c) 2010 Ostravská univerzita v Ostravě. All rights reserved.
//
#include <stdio.h>
#include "charStats.h"
int numberOfLowerCase = 0;
int numberOfUpperCase = 0;
int numberOfOtherChars = -1; // user input will end up with zero
// but we don't want to count that towards the results
char readCharacter () {
printf ("Enter a char: ");
return (getchar ());
}
void addToStats (char character) {
if (character >= 'a' && character <= 'z') {
numberOfLowerCase++;
} else if (character >= 'A' && character <= 'Z') {
numberOfUpperCase++;
} else {
numberOfOtherChars++;
}
}
void printStats () {
printf ("Number of lower-case letters: %d.\n", numberOfLowerCase);
printf ("Number of upper-case letters: %d.\n", numberOfUpperCase);
printf ("Number of other letters: %d.\n", numberOfOtherChars);
}
//
// charStats.h
// Zkouska
//
// Created by Milan Vít on 31. 10. 10.
// Copyright (c) 2010 Ostravská univerzita v Ostravě. All rights reserved.
//
#ifndef CHARSTATS_H
#define CHARSTATS_H
char readCharacter ();
void addToStats (char);
void printStats ();
#endif
//
// main.c
// Zkouska
//
// Created by Milan Vít on 31. 10. 10.
// Copyright (c) 2010 Ostravská univerzita v Ostravě. All rights reserved.
//
#include <stdio.h>
#include "charStats.h"
int main (int argc, const char *argv[]) {
char character;
do {
character = readCharacter ();
addToStats (character);
} while (character != '0');
printStats ();
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment