STRINGS

 

String is an ordered sequence of symbols or array of characters.
character is a letter, numerical digit, or punctuation mark.
Character has it's own number in ASCII code of 256 characters.

a:char;

If you have number of some char in ASCII code, you can use this function :

var
  i:integer;
  a:char;
begin
  a:=chr(i);
end;

CHR function returns char of certain numerical value in ASCII code.

As we said, string is an array of characters, and it can represent
a word, or whole sentence as well as letters and digits.

We declare string like this :

s:string;

There are few operations with strings.
First and most used is function for finding length of string.

you can call that function like this :

  i:integer;
  s:sting;
begin
  i:=length(s);
end;

LENGTH function returns an integer which represents length of string
or length of array of characters or number of characters in one string.

If we want to write separatly in every line every charin one string,
that program would look like this.

program writing_chars_of_string;
  var
    i:integer;
    s:string;
begin
  writeln('Enter any string ?');
  readln(s);
    for i:=1 to length(s) do
       writeln(s[i]);
end.

Every character in one string has it's own number, for example
if string is "pascal", s[2]=a, because second character in word
"pascal" is "a".

That is if we want to separate characters, and if we have to
separate whole parts of string, or separate array of characters
from array of characters, then we use this function :

  s1,s2:string;
begin
  s2:=copy(s1,4,2);
end;

This means that s2 equals s1 from 4th character and 2 characters after that.
From this we can conclude that length(s2)=2;

For example :

s1:='pascal';

and we do this function on s2 and write that on screen :

s2:=copy(s1,1,3);
writeln(s2);

we will get that s2='pas'.

And third function which we use for strings is :

pos(sub-string,string);

POS means "position of string" and that is numerical value
which represents start of sub-string in string.
Value of POS would be 0 if there is not sub-string in string.

Let's write a program which will tell us the position of
sub-string in string and if the sub-string is not in the string,
it will tell us that.

program sub_string;
  var
     k:integer;
     s1,s2:string;
begin
  writeln('Enter sub-string ?');
    readln(s1);
  writeln('Enter string ?');
    readln(s2);
  k:=pos(s1,s2);
    if k=0 then
      writeln('Sub-string is not the part of string !')
    else
      writeln('Position of sub-string in string is ',k);
end.

 

 
 

 

<<BACK                                         NEXT>>

 



         

 

 

 

 

© All rights reserved. Dr DEBCOL. 2009.