#include <stdio.h>
#include <string.h>

int fromhex(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	else if (c >= 'a' && c <= 'f')
		return 10 + c - 'a';
	else if (c >= 'A' && c <= 'F')
		return 10 + c - 'A';
	else return 0;
}

char tohex(int i)
{
	if (i >= 0 && i <= 9)
		return '0' + i;
	else
		return 'A' + (i - 10);
}

void decode(FILE *input)
{
	char c;
	char value;
	int count = 3;
	int meaning;
	while ((c = fgetc(input)) != -1)
	{

		meaning = fromhex(c);
		
		if (c == '%') {
			value = 0;
			count = 0;
			continue;
		}

		value += meaning * (count++?1:16);

		if (count == 2) 
			printf ("%c",value);

		if (count > 2)
			printf("%c",c);
	}
}

void encode(FILE *input)
{
	char c;
	
	while ((c = fgetc(input)) != -1)
		printf("%c%c%c",'%',tohex(c/16),tohex(c%16));
}

main(int argc, char **argv)
{
	int i;
	void (*function)(FILE *) = &encode;
	
	for(i = 1; i < argc; i++)
	{
		if (!strcmp(argv[i],"--decode"))
			function = &decode;
		if (!strcmp(argv[i],"-d"))
			function = &decode;
	}

	(*function)(stdin);
	printf("\n");
}
