GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Dec-2005, 08:27
selent selent is offline
New Member
 
Join Date: Dec 2005
Posts: 13
selent is on a distinguished road
Unhappy

C - converting words into numbers


Hi,
I'm not very experienced in C and i'm trying to write the code for a predictive text program like T9. The program should read in words from words.txt (it's a document with 25143 words), which it's doing correctly i think, and it's meant to return numbers.
Example:
'and'
'for'
should return as: 263
367
At the moment, i'm getting a huge list of numbers which i think are random. The problem is: I don't know what's wrong with the code. It should convert the words one by one into the numbers they represent. I will really appriciate it if someone could tell me what's wrong. I've highlited the 'wrong' parts red.

My code is the following:

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

char* words[25143];

char LowerCaseValue(char letter)
{
   if ((letter >= 'A') && (letter <= 'Z'))
      return (letter + 32);
   else
      return letter;
}

unsigned let2int(char letter)
{
   unsigned result;

   if ((letter >= 'a') && (letter <= 'c'))
   {
      result=2;
   }
   else if ((letter >= 'd') && (letter <= 'f'))
   {
      result=3;
   }
   else if ((letter >= 'g') && (letter <= 'i'))
   {
      result=4;
   }
   else if ((letter >= 'j') && (letter <= 'l'))
   {
      result=5;
   }
   else if ((letter >= 'm') && (letter <= 'o'))
   {
      result=6;
   }
   else if ((letter >= 'p') && (letter <= 's'))
   {
      result=7;
   }
   else if ((letter >= 't') && (letter <= 'v'))
   {
      result=8;
   }
   else if ((letter >= 'w') && (letter <= 'z'))
   {
      result=9;
   }
   return result;
}

[color="red"]void word2numeric(char* word, unsigned numeric[30])
{
   register int i;
   int count=0;

   
for (i=0;i<(int)strlen(word);i++)
   {
      numeric[count]=let2int(LowerCaseValue(word[i]));
      count++;
   }
}[/color]int main(void)

{	
	FILE *data_in;
	unsigned numeric[30];
	char * word;
	int i;
	int count=0;
	char words[25143];

	data_in = fopen("words.txt","r");

	if (data_in == NULL) printf("Cannot open file\n");
	else
	{	
		while (feof(data_in) == 0)
		{
		fscanf(data_in, "%s", words);
		printf("selen");
			
			[color="Red"]word2numeric(word,numeric);
			for (i=0;i<25143;i++)
			{
			printf("%2d",numeric[i]);
			}[/color]		}
	}	
	fclose(data_in);   	
return 0;
}
Last edited by LuciWiz : 12-Dec-2005 at 08:49. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 12-Dec-2005, 09:09
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 331
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: C - converting words into numbers


This will convert a char to its ascii code:

CPP / C++ / C Code:

inline unsigned short char2ascii(char C)
{
   return *((unsigned short*)&C);
}

  #3  
Old 12-Dec-2005, 10:41
selent selent is offline
New Member
 
Join Date: Dec 2005
Posts: 13
selent is on a distinguished road

Re: C - converting words into numbers


I'd like to convert all words into numbers which appear on a mobile phone. i.e
a,b,c = 2
d,e,f = 3
g,h,i = 4 and so on..
  #4  
Old 12-Dec-2005, 11:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: C - converting words into numbers


Define an array of 26 integers corresponding to the letter/number combinations:
CPP / C++ / C Code:
int num[26] = 
   {2,2,2,    // abc 
    3,3,3,    // def
    4,4,4,    // ghi
    5,5,5,    // jkl
    6,6,6,    // mno
    7,7,7,7,  // pqrs
    8,8,8,    // tuv
    9,9,9     // wxyz
   }

In code:
1) get a single character, say chr
2) subtract 65 (A) to move it into the numbers 0-25 (A=65 => 0, B=66 => 1)
3) If chr < 0, it's invalid
4) if chr >= 26 subtract 6 because it's probably lower case
5) If chr < 0 or >= 26, it's invalid
6) num[chr] is your integer value

there is a mathmatical way to do this but it's more difficult.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #5  
Old 12-Dec-2005, 12:14
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 331
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: C - converting words into numbers


CPP / C++ / C Code:

unsigned short char2num(char C)
{
  string alphabet = "abccdeffghiijkllmnoopqrstuvvwxyz";
  return alphabet.find(C) / 4 + 2;
}


This works if you give it a valid input. The repeated characters are deliberate.
  #6  
Old 12-Dec-2005, 12:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: C - converting words into numbers


Quote:
Originally Posted by Blake
CPP / C++ / C Code:

unsigned short char2num(char C)
{
  string alphabet = "abccdeffghiijkllmnoopqrstuvvwxyz";
  return alphabet.find(C) / 4 + 2;
}

Convoluted and obfuscated. Let's try to keep it simple for the new programmers. This code also will not work with upper case letters. And what happens if you pass in the character '%' or '4' -- any non-letter?

Quote:
Originally Posted by Blake after an edit
This works if you give it a valid input. The repeated characters are deliberate.
And don't modify your post after someone comments on it to make their comment invalid. You don't really want to make people look like a dunderhead, do you?
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #7  
Old 12-Dec-2005, 12:26
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 331
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: C - converting words into numbers


You left off d*** clever. (=

Often the cleverest solutions are hardest to understand.

As I said you would need to check for invalid characters before calling the function, but that would apply to any solution to this problem, so I don't see what the big deal there is.

Quote:
And don't modify your post after someone comments on it to make their comment invalid. You don't really want to make people look like a dunderhead, do you?

No one had responded yet when I made the modification.
  #8  
Old 12-Dec-2005, 12:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: C - converting words into numbers


Quote:
Originally Posted by Blake
No one had responded yet when I made the modification.
You must have been editing the post while I was commenting then... It happens...
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #9  
Old 12-Dec-2005, 12:37
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 331
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: C - converting words into numbers


Well, sorry about the confusion.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary number systems: BCD, twos complement, ones complement, etc. machinated Miscellaneous Programming Forum 6 08-Feb-2006 10:51
Converting numbers to words thulaotzu C++ Forum 7 12-Dec-2005 08:17
subscript error in coding warborules C Programming Language 6 27-Nov-2005 17:16
Linear Search eccoflame C Programming Language 3 19-Apr-2005 08:36
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:45.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.