Exemplo n.º 1
0
/*
 * A word-counting function. Counts the words in file_name and returns the number.
 */
int count_words_in_file(char *file_name) {
	unsigned char buffer[FILE_READ_BUFFER_SIZE];
	int i, is_alpha, chars_read, num_words = 0, in_word = 0;
	FILE *file;

	// Open file, and return -1 on error
	file = fopen(file_name, "r");
	if (!file) {
		printf(ERR_CANT_COUNT, file_name);
		return -1;
	}

	printf("%s %s\n", INFO_COUNT, file_name);

	// Read file in chunks until EOF is reached
	while (fgets((char*) buffer, FILE_READ_BUFFER_SIZE, file) != NULL) {
		chars_read = strlen((char*) buffer);

		for (i = 0; i < chars_read; i++) {
			is_alpha = is_alphabetic(buffer[i]);

			// Match, but does not interrupt. counts.
			if (is_alpha && !in_word) {
				num_words++;
				in_word = 1;

			// Otherwise if it's whitespace and we were in a word
			} else if (!is_alpha && in_word) {
				in_word = 0; // change flag to false
			}
		}
	}
	fclose(file);
	return num_words;
}
Exemplo n.º 2
0
BOOL is_valid_key( const char* utf8char, uint16_t input_type)
{
   BOOL latin_alphabethic;
   
   if( !utf8char || !(*utf8char))
   {
     waze_assert(0);
      return FALSE;
   }
   
   latin_alphabethic = ('\0' != utf8char[1]);

   if( inputtype_binary == input_type)
      return TRUE;
      
   // Searching for alpha-bet?
   if( (inputtype_alphabetic & input_type) && (latin_alphabethic || is_alphabetic( *utf8char)))
      return TRUE;
   
   if( latin_alphabethic)
      return FALSE;  // All other tests should fail...
      
   return is_valid_nonalphabetic_char( utf8char[0], input_type);
}      
Exemplo n.º 3
0
// *******************************************************************
// FUNCTION:  total_words
//
// DESCRIPTION: This function will return the number of words in a
//              give string as an integer.
//
// PARAMETERS:  string - an array of characters (string)
//
// OUTPUTS:     int - number of words in the string
//
// CALLS:       is_alphabetic
// *******************************************************************
int total_words (char string[])
{
    int i; // increments the loop
    int words = 0; // Used to store the number of words in the string
    bool looking_for_word = true; // tells if you are still looking for a word or if you've found it
    bool is_alphabetic (const char c); // determines if the letter is alphabetic (function included below)
    
    // loops through each letter of the string and adds to word count if a space is encountered
    for (i = 0; string[i] != '\0'; ++i) {
        if (is_alphabetic(string[i])) {
            if (looking_for_word) {
                ++words;
                looking_for_word = false;
            }
        }
        else
        {
            looking_for_word = true;
        }
    }
    
    // Returns the number of words
    return words;
}
Exemplo n.º 4
0
/*
 * expand: given an input string specifiying ranges of digits and letters
 * 	expand it into the full string and store the result in output. leading and trailing '-' are treated
 * 	literally.
 * 	for example:
 * 	a-z will give abc...xyz
 * 	0-9 will give 012...789
 * 	-a-d- will give -abcd-
 * 	-a-d7-9 will give -abcd789
 * 	etc.
 */
void expand(char input[], char output[]) {
    int digit_range = 0, letter_range = 0;
    int start = 0, end = 0;
    int length;
    int index = 0;
    
    length = string_length(input);
    while (index < length) {
        int current_char = input[index];
        if (!digit_range && !letter_range) { // not in a range
            if (current_char == '-') {
                printf("-");
                ++index;
            } else if (is_alphabetic(current_char)) { // start of a letter range
                letter_range = 1;
                start = current_char;
                end = start;
            } else if (is_digit(current_char)) { // start of a digit range
                digit_range = 1;
                start = current_char;
                end = start;
            } else {
                printf("Error in input!");
                break;
            }
        } else if (letter_range && !digit_range) {
            // Depending on the input there are now several cases while being in a letter range
            // (a) the next char is a '-' and immediately following is a letter:
            //      this is just a vanilla range, so we update the end value of the range
            // (b) the next char is a '-' and immediately following is a digit:
            //      in this case we complete the existing range and signal that we are not
            //      in a letter_range anymore
            // (c) the next char is alphabetic
            //      just expand the range-so-far and set the start point to next char
            // (d) the next char is a digit
            //      end of the letter range-so-far -> expand range and indicate end of letter_range
            // (e) the last char is a '-'
            //      put the value in 'start' into output and indicate end of letter_range
            // (f) still in a letter range and at the end of the string
            //      just expand the current range
            if (index < (length - 2)) { // we still have at least 2 chars to examine
                // cases (a), (b), (c), (d)
                int next = input[index + 1];
                int next_next = input[index + 2];
                if (next == '-' && is_alphabetic(next_next)) {
                    // (a)
                    if (next_next > end) {
                    end = next_next;
                    }
                    index += 2;
                } else if (next == '-' && is_digit(next_next)) {
                    // (b)
                    print_range(start, end);
                    letter_range = 0;
                    ++index;
                } else if (is_alphabetic(next)) {
                    // (c)
                    print_range(start, end);
                    start = next;
                    end = start;
                    ++index;
                } else if (is_digit(next)) {
                    // (d)
                    print_range(start, end);
                    letter_range = 0;
                    ++index;
                }
            } else if (index < length - 1) {    // there is at least 1 char remaining
                // (e)
                int next = input[index + 1];
                if (next == '-') {
                    print_range(start, end);
                    letter_range = 0;
                    ++index;
                }
            } else {    // we are at the end of the string
                // (f)
                print_range(start, end);
                break;
            }
        } else if (digit_range) { // we are in a digit range now
            // Look above for the reasoning behind the cases - apply them analogously
            if (index < (length - 2)) {
                // cases (a), (b), (c), (d)
                int next = input[index + 1];
                int next_next = input[index + 2];
                if (next == '-' && is_digit(next_next)) {
                    // (a)
                    if (next_next > end) {
                    end = next_next;
                    }
                    index += 2;
                } else if (next == '-' && is_alphabetic(next_next)) {
                    // (b)
                    print_range(start, end);
                    digit_range = 0;
                    ++index;
                } else if (is_digit(next)) {
                    // (c)
                    print_range(start, end);
                    start = next;
                    end = start;
                    ++index;
                } else if (is_alphabetic(next)) {
                    // (d)
                    print_range(start, end);
                    digit_range = 0;
                    ++index;
                }
            } else if (index < length - 1) {    // there is at least 1 char remaining
                // (e)
                int next = input[index + 1];
                if (next == '-') {
                    print_range(start, end);
                    letter_range = 0;
                    ++index;
                }
            } else {    // we are at the end of the string
                // (f)
                print_range(start, end);
                break;
            }
        }
    }	
}