Sign Guestbook
View Guestbook
Invite Your Friends
Website Awards
Message Forum
Source Codes
Downloads
Links
About Me
 
     
Lesson 1 - The first few steps in Pascal Programming
Lesson 2 - Variables, Constants and the Assignment  Statement
Lesson 3 - Special Functions: ClrScr(), GotoXy(), etc...
Lesson 4 - Program Control
Lesson 5 - The CASE-OF Statement
Lesson 6 - Logical Operators and Boolean Expressions
Lesson 7 - Procedures and Functions
Lesson 8 - BGI Graphics
Lesson 9 - File Handling
Lesson 10 - Arrays
Lesson 11 - Record Data Structure
Lesson 12 - Strings

Add this site to your Favourites

   



 


Lesson 12
- Strings

 

This lesson will cover:

 

 

 

Introduction to Strings

Maybe you already know what is a string variable from previous program examples that you have tried, but you don't know what operations and what functions one can apply to them and how can they be manipulated in order to obtain another form of string whatsoever.

In this lesson we will cover some important functions that the Pascal programming language has for us in order to cater for string operations far more easier to use than you think.

Let's jump into the strings staff straight away. In order to understand strings, one has to keep in mind that a string is made up of an array of characters. The string data type is an in-built data type that is an array of 256 characters (Type String = Packed Array[0..255] of Char). When stored in memory, the processor should know where the string starts and where it finishes. In order to know where the string finishes, in Pascal, the 0th element of a string is defined as the length of the string. So, if you try to access character 0 of a string, the number of characters stored in that array is returned, thus letting the processor to know where the string finishes.

The following example shows some simple string operations.

Var
    myString : String;

Begin
 myString := 'Hey! How are you?';
 Writeln('The length of the string is ',byte(myString[0]));
 Write(myString[byte(myString[0])]);
 Write(' is the last character.');
End.

Note that in the previous program I have used an automatic data-type conversion, normally referred to as data type-casting. When type-casting from one data type to another, all that is happening is simply a conversion from one data type to another based on the data type in subject and the wrapping data type to which the old data type is being converted. In our case, the array variable myString has a special 0th element storing the number of characters in that array in string format. This value is an ascii value, so trying to display it without converting it into a number, the alternative ascii character is displayed. So you have to change the character value to an ordinary number by wrapping the variable by another data type, in our case a byte data type (why use integer? - its all waste of memory and memory consumption).

Note that myString[myString[0]] without having data-type casting around myString[0] will lead to a compiler error because myString[0] is a character and not an integer.

Note that to retrieve the length of a string (ie. the number of characters in a string) there is no need to use the method I showed to you because its too complicated and unfriendly. Instead, there is the simpler function, length() which will return the number of characters of the string in subject. Eg. numOfChars := length(myString);

Some Useful String Functions

There are some basic Pascal functions that has to do with string operations and so there is no need to write them yourself, and they are of course quite useful. I will explain all the string functions by describing what they do and a simple example of each.

Function Pos(SubString : String; S : String) : Byte;

Description

This function will search for the string SubString within the string S. If the sub-string is not found, then the function would return 0. If on the other hand the sub-string is found, then the index integer value of the first character of the main string that matches the character of the sub-string is returned.

Var 
   S : String;

Begin
 S := 'Hey there! How are you?';
 Write('The word "How" is found at char index ');
 Writeln(Pos('How',S));
 If Pos('Why',S) <= 0 then
  Writeln('"Why" is not found.');
End.

Function Copy(S : String; Index : Integer; Count : Integer ) : String;

Description

This function will copy some characters from string S starting from character index Index and copies as much as Count. The copied string is then returned.

Var 
   S : String;

Begin
 S := 'Hey there! How are you?';
 S := Copy(S, 5, 6); { 'there!' }
 Write(S);
End.

Procedure Delete(var S : String; Index : Integer; Count : Integer );

Description

Deletes a specified number of characters from the string S. The starting position of deletion is from character index Index. The number of characters that will be deleted is specified through Count. The new string is passed back through the variable parameter S.

Var 
   S : String;

Begin
 S := 'Hey Max! How are you?';
 Delete(S, 4, 4); { 'Hey! How are you?' }
 Write(S);
End.

Procedure Insert(Source : String; var S : String; Index : Integer);

Description

This function will insert a string of any length into a source string starting from an index character. The string S will be inserted into string Source starting from the index character Index. No characters will be deleted from the front except if the resulting string is longer than 255 characters. In this case, the front characters will be truncated as to fit a 255-character string.

Var 
   S : String;

Begin
 S := 'Hey! How are you?';
 Insert(S, ' Max', 4);
 Write(S);
  { 'Hey Max! How are you?' }
End.

Function Concat(s1 [, s2, s3...sn] : String) : String;

Description

Concatenates 2 or more strings depending how long is the argument expression. Try to make sure not to exceed the limit of 255 characters when concatening strings as it will result in truncation. This function can also be obtained by using the plus (+) operator between strings that need to be concatenated.

Var 
   S1, S2 : String;

Begin
 S1 := 'Hey!'
 S2 := ' How are you?';
 Write(Concat(S1, S2)); { 'Hey! How are you?' }
End.

is the same as

Var 
   S1, S2 : String;

Begin
 S1 := 'Hey!'
 S2 := ' How are you?';
 Write(S1 + S2); { 'Hey! How are you?' }
End.

Function UpCase(C : Char) : Char;

Description

Converts the character C to uppercase and returned. If the character is already in uppercase form or the character is not within the range of the lower case alphabet, then it is left as is.

Var 
   S : String;
   i : Integer;

Begin
 S := 'Hey! How are you?';
 For i := 1 to length(S) do
  S[i] := UpCase(S[i]);
 Write(S); { 'HEY! HOW ARE YOU?' }
End.

Procedure Str(Val : Integer / LongInt / Real; var S : String);

Description

Converts an integer or a decimal value to a string. The value parameter Val is converted into a string and passed through the variable paramter S.

Var 
   S : String;
   i : Real;

Begin
 i := -0.563; 
 Str(i, S);
 Write(S); 
End.

Procedure Val(S : String; var Val; Code : Integer);

Description

Converts a string to its corresponding numeric value. The string paramater S is converted into a numeric value and passed back through the variable paramter Val. If the string to be converted is not a correct numeric value, an error occurs and is returned via Code. If the conversion is correct then Code is 0.

Var 
   S     : String;
   error : Integer;
   R     : Real;

Begin
 S := '-0.563'; 
 Val(S, R, error);
 If error > 0 then
  Write('Error in conversion.')
 Else
  Write(R); 
End.

 
 


User Comments

Post By: JIGGA Posted On: 2009-11-05
This is very informative.......KEEP IT UP!!!

Post By: njuguna Posted On: 2009-09-08
hot

Post By: Syko Pyro Posted On: 2009-07-13
In the pos majoobadahoobie at the top do you inadvertantly go from searching for the word how, to the word why?

Post By: acid Posted On: 2009-07-08
wow.....very nice post !... your post helped me ,again..thanks ! :D

Post By: Ashanthi Posted On: 2009-06-07
Thanks a lot for this really helpful lesson.

Post By: Muhedin Abdullahi Mohamed Posted On: 2009-05-16
this is really a usefull site, but I see it may be better to organise the lessons more than it is; for example giving a detailed explanation about a particular structure of the language followed by a number of examples increasing in default the simplest one first,then the second simplest......and lastly the most deficult one.....that is my poor point of view.

Post By: Mike Posted On: 2009-03-14
Wow. This is VERY informative! I implore any young programmer to take the time out and give it a read.

Post By: Posted On: 2009-01-14
Your Comment

Post By: samz Posted On: 2008-12-20
wish i saw these lessons before my exam :(

Post By: Chris Cox Posted On: 2008-08-19
Thinking of printing this course so I'll have a hardcopy.

Post By: Mack Posted On: 2008-07-30
Having trouble downloading rar files from your site!

Post By: Mack Posted On: 2008-07-30
Great Site!!

Post By: Roh Posted On: 2008-05-29
Here's some code if you need To convert String to PChar // the inclusions uses Strings; ... // the declarations MyStrMsg : String; MyPCharMsg : PChar; ... ... // First, Null terminate the string MyStrMsg := MyStrMsg + Char(0); // Second, Assign the address of the 1st byte of String to PChar variable MyPCharMsg := @MyStrMsg[1]; // That's it!

Post By: demjan Posted On: 2008-05-06
Hi again.I just want say that you have mistake with FUNCTION Insert.That is a function so it must return some result so you need to give someone that result. example: Var S,s2 : String; Begin S := 'Hey there! How are you?'; //you can add new variable like s2 S2 := Copy(S, 5, 6); { 'there!' } Write(S2); //or just Write(Copy(S1,5,6)); End. (Delete() and Insert() are procedures so they don't have to return some result.) and you have mistake with procedure Insert().In you example it need to go: Var S : String; Begin S := 'Hey! How are you?'; Insert(' Max', S, 4); //you write Insert(S,' Max',4) Write(S); { 'Hey Max! How are you?' } End. I am sorry if i am boring, but i don't want to someone learn bad.This is very good tutorial.It helped me a lot.

Post By: ashkan-kho Posted On: 2008-03-30
how can i resive data from USB port and insert it in my progaram

Post By: mutindi Posted On: 2008-03-06
this are good notes please may i know more on pointers somoni, Kenya

Post By: gokhanaygun Posted On: 2007-12-30
tank you for pascal lessons

Post By: moderator Posted On: 2007-11-14
Mistakes in Insert() and Delete() were corrected thanks to Demjan.

Post By: Demjan Posted On: 2007-08-17
You have mistake with procedure delete and insert.You need first to write delete(s,index,count) and then write(s),it can't go together(that is in procedure delete).In procedure insert you need to write first insert('...',s,index) and then write(s)(that is in procedure insert).I have some error with procedure val and str and i don't know why.Can you answer me?

Post By: Aubrey Bupe Posted On: 2007-07-31
I would love you to post me a demonstration code that is able to implement a program for crating three different types of bank accounts,allowing depositing,withdrawing,deleting accounts whose balances are below the book balance automatically and prints statements at the end of the month

Post By: Posted On: 2007-05-12
It's too complex!

Post By: CatCat Posted On: 2007-03-22
People who want to learn pascal must go to this website.

Post By: Tommy Posted On: 2007-01-31
Wow! This tutorial really helped me! Especially the graphics and string part! They didn't teach us graphics at school.

Post By: lucky wickramasinghe-sri lanka Posted On: 2006-12-12
thanks a lot again & again! i am sorry to say I have less knowledge to correct your errors as I am a student still!But in near future I will become an excellent programer and I will say to the whole world how you helped me!

Post By: imo Posted On: 2006-11-25
keep it up guy

Post By: José Muñoz Posted On: 2006-08-23
Your site has helped me a lot. Thanks.

Post By: elstan Posted On: 2006-08-07
hey there are syntax and typing errors in the concat function. But overall,this site is fabulous!

Post By: anjini Posted On: 2006-07-28
ur site helps a lot, thxs especially for students like us where we really need examples to guide us, haven't seen all yet but till far, its really good thxs agn

Post By: Marlon/madwizkid Posted On: 2006-07-18
It's very informative for beginners


Post Your Comment on this Article!
Your Name/Nickname:
Your Email:
Your Comment:
Insert the BLUE numbers shown below in the text field on the right. [ This is an anti-spam counter-measure ]
35328161267
 
 
[HOME][LESSON INDEX][INVITE A FRIEND][FORUM][ABOUT ME]

modified last: November 2006 (Article Version: 2)

mail to: victorsaliba@hotmail.com

© Victor John Saliba 2006  
| Privacy Statement | Disclaimer | Contact Us |
 
 
 
www.pascalprogramming.byethost15.com © Victor John Saliba 2006