Hello Everyone,

I am trying to emulate a T9 (cellphone) keyboard in C. So lets say a user types in the numbers "23", a possible word combination would be "be". In my question here, I will only write down a part of what I have assuming it only works for the numbers 2, 3 (for simplicity - but my full code is posted below). The number 2 in T9 keyboard has the letters "a,b,c" and the number 3 has the letters "d,e,f".

So I have a word.txt file which has a list of sorted dictionary words.
I also have an array that contains characters. So I have the following:

char charArray2[3] = {'a', 'b', 'c'}; 
char charArray3[3] = {'d', 'e', 'f'};

char *numToChar[10];

char *array2;
array2 = &charArray2[0];

char *array3;
array3 = &charArray3[0];
numToChar[2] = array3;


numToChar[2] = array2;
numToChar[3] = array3;

Now let's assume as I said earlier, the user input the numbers 2 and 3. I should access numToChar, which is an array that holds pointers to my char arrays, and then use that pointer to access the char array of letters.

So:

char *ptr;
ptr = numToChar[2];

That way, I have a char pointer "ptr" that points to charToArray2[0]. What I need to do is grab all the letters in that array, which are a,b,c and then just filter the file to the words that start with either a or b or c only.

Then when 3 is read in, ptr will point to numToChar[3], and will filter the words only to those words that start with either a,b,c and have a second letter of d,e,f.


I have written everything up to that point. I am trying to do a binary search in the file, but I'm not sure how to do that. How would you recommend I deal with those strings in the file? How can I run a binary search within the actual file and then just read the first letter of the string in that file? I'm not sure how to do that - I've read a lot about functions such as strncmp, strtok and fseek but I don't think any of those are useful to me?

Below is my full code so far, and any help is appreciated. Thank you

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {

	char charArray2[3] = {'a', 'b', 'c'};
	char charArray3[3] = {'d', 'e', 'f'};
	char charArray4[3] = {'g', 'h', 'i'};
	char charArray5[3] = {'j', 'k', 'l'};
	char charArray6[3] = {'m', 'n', 'o'};
	char charArray7[4] = {'p', 'q', 'r', 's'};
	char charArray8[3] = {'t', 'u' , 'v'};
	char charArray9[4] = {'w', 'x', 'y', 'z'};
	char *numToChar[10];

	// Create map
	char *array2;
	array2 = &charArray2[0];
	numToChar[1] = array2;

	char *array3;
	array3 = &charArray3[0];
	numToChar[2] = array3;

	char *array4;
	array4 = &charArray4[0];
	numToChar[3] = array4;

	char *array5;
	array5 = &charArray5[0];
	numToChar[4] = array5;

	char *array6;
	array6 = &charArray6[0];
	numToChar[5] = array6;

	char *array7;
	array7 = &charArray7[0];
	numToChar[6] = array7;

	char *array8;
	array8 = &charArray8[0];
	numToChar[7] = array8;

	char *array9;
	array9 = &charArray9[0];
	numToChar[8] = array9;

	// Take input from user
	int num;
	scanf("%d", &num);
	int numTemp = num;

	// Open File
	FILE *fp;
	fp = fopen("wordlist.txt", "r");
	if (fp == NULL) {
		printf("Error opening dictionary.");
		exit(1);
	}

	int numLength = ceil(log10(num));
	int *numArray;
	numArray = malloc (numLength * sizeof(int));

	// Insert digits in array in order of what the original number is
	for (int i = numLength-1; i >= 0; i--) {
		numArray[i] = numTemp % 10;
		numTemp = numTemp / 10;
	}

	// Fetch numbers from numArray and access their charArray
	for (int i = 0; i < numLength; i++) {
		int currDigit = numArray[i];
		char *ptr;
		ptr = numToChar[currDigit-1];

		while (!feof(fp)) {
			// ??
		}


	}

	// Dealloc and close file
	free(numArray);
	numArray = NULL;
	fclose(fp);

	/*int numTemp;
	while (numTemp != 0) {
		numTemp % 10;
	}*/


}

Anyone?

If you are going to binary search a file you need to do one of two things.
A) Store all entries as fixed-width (That way you can calculate jump offsets precisely)
B) Use the current file position to jump to a new location (based on file size) and search for the next or previous newline.

I'd suggest you build a trie with the input letters and based on the words that are contained in that sort them and search your file in one loop instead of trying to binary search a text file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.