Problem: Characters

You’re interested in figuring out the number of case-insensitive unique characters in some string. This sounds simple and easily-automated, so you just decide to just go for it.

Input

Your input will consist of an integer N followed by a newline. N test cases will follow, all separated by newlines. Each test case is a string of letters containing some combination of letters (upper- and lower-case are allowed), the space character, and the characters contained in the string ?!.’$%&*:;,.

Output

Your output should consist of one integer per case, separated by whitespace, indicating the number of case-insensitive unique characters contained in the input string.

Constraints

5 ≤ N ≤ 25
Input strings will not exceed 100 characters in length.

Sample input:

5
Lorem ipsum
Eclipse is a pretty nice editor.
This is not a very interesting case.
The quick brown fox jumps over the lazy dog
Bonk.

Sample output:

10
15
15
27
5

The code (C):

/*
 ============================================================================
 Name        : characters-c.c
 Author      : Francesco Laurita
 Version     : 1.0
 Copyright   : Francesco Laurita
 Description :
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CACHE_SIZE 50
char cache[CACHE_SIZE] = { '\0' };

void add(char c) {
	char *chr = strchr(cache, c);
	//if char is not present into the current cache, it will be appended
	if (chr == NULL)
		cache[strlen(cache)] = c;
}
int main(int argc, char **argv) {

	FILE *fp = fopen(argv[1], "r");
	if (!fp) {
		fprintf(stderr, "Unable to open file\n");
		return EXIT_FAILURE;
	}
	char current;
	//Skip the first line
	while((current = fgetc(fp)) != '\n'){;}

	//Read the whole file char by char
	while (1) {

		current = fgetc(fp);

		if (current != '\n' && current != EOF) {
			add(tolower(current));
		} else {//EOL or EOF
			printf("%d\n", (int) strlen(cache));
			bzero(cache, CACHE_SIZE);
		}

		if (current == EOF)
			break;
	}

	fclose(fp);
	return EXIT_SUCCESS;
}

PHP Version:

#!/usr/bin/php
<?php
$data = file($argv[1]);
if (!$data) exit(1);
array_shift($data);
foreach ($data as $row){
    $row = strtolower(trim($row));
    echo count(array_unique(str_split($row))) . "\n";
}
?>
© 2013 Francesco Laurita' s blog Suffusion theme by Sayontan Sinha