Words To Numbers

This algorithm converts English words to decimal numbers.



									/*****Please include following header files*****/
// stdint.h
// math.h
// string.h
/***********************************************/

char* GetSubString(char* str, int index, int count) {
	int strLen = strlen(str);
	int lastIndex = index + count;

	if (index >= 0 && lastIndex > strLen) return "";

	char* subStr = malloc(count + 1);

	for (int i = 0; i < count; i++) {
		subStr[i] = str[index + i];
	}

	subStr[count] = '\0';

	return subStr;
}

char* AppendString(const char* str1, const char* str2) {
	int str1Len = strlen(str1);
	int str2Len = strlen(str2);
	int strLen = str1Len + str2Len + 1;
	char* str = malloc(strLen);

	for (int i = 0; i < str1Len; i++)
		str[i] = str1[i];

	for (int i = 0; i < str2Len; i++)
		str[(str1Len + i)] = str2[i];

	str[strLen - 1] = '\0';

	return str;
}

char* RemoveString(char* str, int index, int count) {
	int strLen = strlen(str);
	int lastIndex = index + count;

	char* s = GetSubString(str, 0, index);
	s = AppendString(s, GetSubString(str, lastIndex, strLen - lastIndex));

	return s;
}

void RTrim(char* str) {
	size_t n;
	n = strlen(str);

	while (n > 0 && isspace((unsigned char)str[n - 1])) {
		n--;
	}

	str[n] = '\0';
}

void LTrim(char* str) {
	size_t n;
	n = 0;

	while (str[n] != '\0' && isspace((unsigned char)str[n])) {
		n++;
	}

	memmove(str, str + n, strlen(str) - n + 1);
}

void Trim(char* str) {
	RTrim(str);
	LTrim(str);
}

uint64_t WordsToNumbers(char* words) {
	if (words == "") return 0;
	Trim(words);
	words = AppendString(words, " ");

	uint64_t number = 0;
	char* singles[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
	char* teens[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
	char* tens[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty" };
	char* powers[] = { "", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion" };
	int singlesLength = 10;
	int teensLength = 10;
	int tensLength = 10;
	int powersLength = 7;

	for (int i = powersLength - 1; i >= 0; i--)
	{
		if (powers[i] != "")
		{
			int index = strstr(words, powers[i]) - words;

			if (index >= 0 && words[index + strlen(powers[i])] == ' ')
			{
				uint64_t count = WordsToNumbers(GetSubString(words, 0, index));
				number += count * (uint64_t)pow(1000, i);
				words = RemoveString(words, 0, index);
			}
		}
	}

	{
		int index = strstr(words, "hundred") - words;

		if (index >= 0 && words[index + strlen("hundred")] == ' ')
		{
			uint64_t count = WordsToNumbers(GetSubString(words, 0, index));
			number += count * 100;
			words = RemoveString(words, 0, index);
		}
	}

	for (int i = tensLength - 1; i >= 0; i--)
	{
		if (tens[i] != "")
		{
			int index = strstr(words, tens[i]) - words;

			if (index >= 0 && words[index + strlen(tens[i])] == ' ')
			{
				number += (unsigned int)(i * 10);
				words = RemoveString(words, 0, index);
			}
		}
	}

	for (int i = teensLength - 1; i >= 0; i--)
	{
		if (teens[i] != "")
		{
			int index = strstr(words, teens[i]) - words;

			if (index >= 0 && words[index + strlen(teens[i])] == ' ')
			{
				number += (unsigned int)(i + 10);
				words = RemoveString(words, 0, index);
			}
		}
	}

	for (int i = singlesLength - 1; i >= 0; i--)
	{
		if (singles[i] != "")
		{
			int index = strstr(words, AppendString(singles[i], " ")) - words;

			if (index >= 0 && words[index + strlen(singles[i])] == ' ')
			{
				number += (unsigned int)(i);
				words = RemoveString(words, 0, index);
			}
		}
	}

	return number;
}
								


Example

									char data[] = "five quadrillion four hundred seventy eight trillion seven hundred seventy five billion five hundred forty four million eight hundred seventy nine thousand five hundred ninty nine";
uint64_t value = WordsToNumbers(data);
								


Output

									5478775544879599