A YouTube user, from the Azerbaijan writes:


I can’t divide numbers in sentences. For example I write "BAKU123". I want to divide "123" from "BAKU"



There are a few things to note in this program.


  1. 1)length(string) returns the length of the string.
    For example:
    sentence := BAKU123
    length(sentence) will return 7 because there are seven characters in the string.

  2. 2)ord() returns the ASCII value of the character. If the ASCII value is 48 - 57 we know that character is a number. If the value is outside that range, then the character is a non-number.

  3. 3)For more information on ASCII please go to: http://en.wikipedia.org/wiki/ASCII



Video: Pascal Helper Series Video 3 - Separating Numbers From Non-numbers In A String
































The code used in this tutorial:


program Project1;


{$mode objfpc}{$H+}


uses

  crt,

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var                                 

  sentence, numbers,  nonnumbers : string;

  x : integer;


begin

   clrscr();

   write('Enter Sentence: ');

   readln(sentence);


   numbers := '';

   nonnumbers := '';      


   for x := 1 to length(sentence) do

   begin

     if (ord(sentence[x]) >= 48) and (ord(sentence[x]) <= 57) then

       numbers := numbers + sentence[x]

     else

       nonnumbers := nonnumbers + sentence[x];

   end;


   writeln;

   writeln('The Numbers Are: ', numbers);

   writeln('The NonNumbers Are: ', nonnumbers);


   writeln;

   writeln;

   writeln('Press <Enter> To Quit');

   readln();

end.





Questions for this series can be submitted at YouTube Personal Message System. Our YouTube name is SchoolFreeware

Previous                 Next