Catching and changing numbers

Hi guys,,, i really need some help in here. I am currently doing a program in pascal which requires me to get all the number digits on a sentence and change this to words. An example output og this program works like this....

Sentence Input : 1 little 2 little 3
Output: one little two little three


1.now my problem is how can i search for all those numbers on a sentence? and;
2. How can i rename this number digits? The range would be 0..9999 so i need some smart programming to do to be able to insert words like 'thousands', 'hundreds' etc. on the english converted digits.

Please me an idea on what to do. Im new in Pascal. Thank you very much in advance.

Comments

  • : Hi guys,,, i really need some help in here. I am currently doing a program in pascal which requires me to get all the number digits on a sentence and change this to words. An example output og this program works like this....
    :
    : Sentence Input : 1 little 2 little 3
    : Output: one little two little three
    :
    :
    : 1.now my problem is how can i search for all those numbers on a sentence? and;
    : 2. How can i rename this number digits? The range would be 0..9999 so i need some smart programming to do to be able to insert words like 'thousands', 'hundreds' etc. on the english converted digits.
    :
    : Please me an idea on what to do. Im new in Pascal. Thank you very much in advance.
    :

    [Problem #1]
    Use a FOR loop such as:
    [code]
    For X := 1 to Length(Sentance) Do
    If Sentance[X] In['0'..'9'] Then ...
    [/code]

    [Problem #2]
    Use DELETE and INSERT:
    [code]
    CONST
    Number : Array[0..9] Of String =
    'Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine';

    Begin
    Insert(Number[X], Sentance, X+1);
    Delete(Sentance,X,1);
    End;
    [/code]

    Sorry about the short reply, in a rush here. Hope this helps a bit.

    Phat Nat
  • [b][red]This message was edited by tron at 2003-2-12 3:19:32[/red][/b][hr]
    : : Hi guys,,, i really need some help in here. I am currently doing a program in pascal which requires me to get all the number digits on a sentence and change this to words. An example output og this program works like this....
    : :
    : : Sentence Input : 1 little 2 little 3
    : : Output: one little two little three
    : :
    : :
    : : 1.now my problem is how can i search for all those numbers on a sentence? and;
    : : 2. How can i rename this number digits? The range would be 0..9999 so i need some smart programming to do to be able to insert words like 'thousands', 'hundreds' etc. on the english converted digits.
    : :
    : : Please me an idea on what to do. Im new in Pascal. Thank you very much in advance.
    : :
    :
    : [Problem #1]
    : Use a FOR loop such as:
    : [code]
    : For X := 1 to Length(Sentance) Do
    : If Sentance[X] In['0'..'9'] Then ...
    : [/code]
    :
    : [Problem #2]
    : Use DELETE and INSERT:
    : [code]
    : CONST
    : Number : Array[0..9] Of String =
    : 'Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine';
    :
    : Begin
    : Insert(Number[X], Sentance, X+1);
    : Delete(Sentance,X,1);
    : End;
    : [/code]
    :
    : Sorry about the short reply, in a rush here. Hope this helps a bit.
    :
    : Phat Nat
    :

    here's a function that converts a number (16bit int, >=0) to its word-representation:

    [code]
    function numberToWords(num : Integer) : String;
    const nums1 : array[0..9] of String = ('zero','one','two','three','four','five','six','seven','eight','nine');
    nums10 : array[10..19] of String = ('ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
    nums20 : array[2..9] of String = ('twenty','thirty','fourty','fivty','sixty','seventy','eighty','ninety');

    var res : String;
    begin
    res := '';

    //thousands
    if (num >= 1000) then begin
    res := numberToWords(num DIV 1000) + '-thousand';
    num := num MOD 1000;
    end;

    //hundreds
    if (num >= 100) then begin
    res := res + ' ' + numberToWords(num DIV 100) + '-hundreds';
    num := num MOD 100;
    end;

    //tens and ones
    if (num >= 20) then begin
    res := res + ' ' + nums20[num DIV 10];
    num := num MOD 10;
    //do not output things like "twenty zero"
    if (num > 0) then res := res + ' ' + nums1[num];

    end else if (num >= 10) then begin
    res := res + ' ' + nums10[num];

    end else res := res + ' ' + nums1[num];

    numberToWords := res;
    end;
    [/code]

    To run through the sentence, you can use a simple loop:

    [code]
    var ins : String; //the sentence
    outs : String; //the output

    p, n : Integer;
    isinnum : Boolean;
    num : String;

    dummy : Word;

    begin

    isinnum := false;
    num := '';

    for p:=1 to length(ins) do begin
    if (ins[p] in ['0'..'9']) then begin
    isinnum := true;
    num := num + ins[p];

    end else begin
    if (isinnum) then begin
    val(num, n, dummy);
    outs := outs + numberToWords(n);

    isinnum := false;
    num := '';
    end;

    outs := outs + ins[p];
    end;
    end;

    if (isinnum) then begin
    val(num, n, dummy);
    outs := outs + numberToWords(n);
    end;

    end;
    [/code]

    (code is not checked)

    tron.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion