![]() |
|
#1
|
|||
|
|||
C - converting words into numbersHi,
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:
Last edited by LuciWiz : 12-Dec-2005 at 08:49.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
||||
|
||||
Re: C - converting words into numbersThis will convert a char to its ascii code:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: C - converting words into numbersI'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
|
||||
|
||||
Re: C - converting words into numbersDefine an array of 26 integers corresponding to the letter/number combinations:
CPP / C++ / C Code:
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
|
||||
|
||||
Re: C - converting words into numbersCPP / C++ / C Code:
This works if you give it a valid input. The repeated characters are deliberate. |
|
#6
|
||||
|
||||
Re: C - converting words into numbersQuote:
Quote:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#7
|
||||
|
||||
Re: C - converting words into numbersYou 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:
No one had responded yet when I made the modification. |
|
#8
|
||||
|
||||
Re: C - converting words into numbersQuote:
__________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#9
|
||||
|
||||
Re: C - converting words into numbersWell, sorry about the confusion.
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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