Created
March 14, 2012 03:15
-
-
Save duraz0rz/2033750 to your computer and use it in GitHub Desktop.
revised edition of textcount.c
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
#include <stdio.h> | |
int main() | |
{ | |
// Initialize to some arbitrary value so we avoid memory and comparison problems right off the bat | |
char currentChar = '\t'; | |
int symbolCount = 0; | |
int numberCount = 0; | |
int firstTime = 1; | |
while (currentChar != EOF) | |
{ | |
// Get the first character, or if it looped back around, the next character because we ran into a whitespace or something. | |
currentChar = fgetc(stdin); | |
// If two consecutive characters are "0x", consume characters until we hit a non-hexadecimal character (G-Z) | |
if (currentChar == '0') | |
{ | |
// We know the currentChar is a number, so it's safe to increment numberCount here. | |
numberCount++; | |
// Grab the next character and see if it's an 'x' | |
currentChar = fgetc(stdin); | |
if (currentChar == 'x') | |
{ | |
// Aha! It's an 'x', meaning that the next sequence of alphanumeric characters indicate a hexadecimal number | |
// Keep consuming characters until we find one that isn't one. | |
currentChar = fgetc(stdin); | |
while ( ((currentChar >= 'A') && (currentChar <= 'F')) || | |
((currentChar >= 'a') && (currentChar <= 'f')) || | |
((currentChar >= '0') && (currentChar <= '9')) ) | |
currentChar = fgetc(stdin); | |
} | |
else | |
{ | |
// Not an 'x', so check to see if it's a number and consume digits until we're done if it is | |
while ( (currentChar >= '0' && currentChar <= '9')) | |
currentChar = fgetc(stdin); | |
} | |
} | |
firstTime = 1; | |
// Note that, even if we entered the last if statement, that we did not check the currentChar. | |
// Check to see if it's a symbol at this point | |
while ( ((currentChar >= 'A') && (currentChar <= 'Z')) || | |
((currentChar >= 'a') && (currentChar <= 'z')) ) | |
{ | |
if (firstTime == 1) | |
symbolCount++; | |
firstTime = 0; | |
currentChar = fgetc(stdin); | |
} | |
firstTime = 1; | |
// Still need to check for the normal number case. The one inside the special case only handle cases where a zero appears first. | |
while ((currentChar >= '0' && currentChar <= '9')) | |
{ | |
if (firstTime == 1) | |
numberCount++; | |
firstTime = 0; | |
currentChar = fgetc(stdin); | |
} | |
} | |
printf("\nThe symbol count is %d and the number count is %d\n", symbolCount, numberCount); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment