COMPUTER STUDIES

STRING FUNCTIONS

The major string handling functions in BASIC programming language are:

  1. ASC function
  2. CHR$ function
  3. LEFT$ function
  4. RIGHT$ function
  5. MID$ function
  6. VAL function
  7. STR$ function
  8. LEN function
  9. DAT$ function
  10. POS function
  11. SEG$ function

 

1) ASCII FUNCTION (ASC)

The ASC function converts a one character string to its coded number.

Format

ASC (A$)

ASC (A$) returns the numeric decimal value of the ASCII code of a single character within the argument or, in case of a string, the ASCII value of the first character of the string.

 

Example

10 LET A$ =”D”

20 PRINT ASC (A$)

 

The ASCII code for character D is 68, so when the program segment run, the value 68 is printed.

 

 

2) CHARACTER FUNCTION (CHR$)

The character function is denoted by CHR$. CHR$ (A) returns a single character whose ASCII value is A. A is a numeric constant, variable or expression that has a value between the limit of ASCII code 0 d” A d” 127.

Format

CHR$ (A)

The function CHR$ serves the opposite of ASC, and is useful in outputting ASCII characters as part of PRINT statement.

 

Example

10 PRINT CHR$ (100)

The character d will be printed since the ASCII code of d is 100.

 

 

3) LEFT FUNCTION (LEFTS)

The LEFT$ function returns the leftmost B character from the string A$

Format

LEFT$ (A$,B)

 

Example

10 LET A$ =”A B C D E F G H I J K L M N O P Q R S T U V W X Y Z”

20 PRINT LEFT$ (A$,8)

 

The output will be ABCDEFGH

 

 

4) RIGHT FUNCTION (RIGHTS)

The RIGHT$ function returns the rightmost character from the string AS.

Format

RIGHT$ (A$, B)

It should be noted that the string expression can be any string variable, string constant or string expression. If B = 0, the null string is return.

 

Example

10 LET A$ =”A B C D E F G H I J K L M N O P Q R S T U V W X Y Z”

20 PRINT RIGHT$ (A$, 10)

 

The output will be QRSTUVWXYZ

 

 

5) MIDDLE FUNCTION (MID$)

The MID$ function returns a string of length characters from string expression beginning with the character.

Format

MID$ (A$, B, C)

Thus, returns a sub – string of A$, starting at character position B and containing C character.

The parameter B, C used in the functions can be numeric constant, variables or expressions. The string expression can be any string variable, string constant, or string expression. If B is greater than the number of string expression, MID$ returns a null string.

 

Example

10 LET A$=”A B C D E F G H I J K L M N O P Q R S T U V W X Y Z”

20 PRINT MID$ (A$, 14,8)

 

The output will be NOPORST

The fourteen character of A$ is N, this is the beginning of the sub – string which contains seven characters.

 

 

6) Value Function (VAL)

The VALUE function converts a string to numeric data. The VAL (A$) function returns a numeric constant equivalent to the number represented by the string A$. The string A$ should consist of all digits.

Format

VAL (A$)

Where

A$ is a string constant or string variable made up of those values BASIC recognizes as acceptable when inputting numeric data:

+ or – sign

Digits 0 through 9

The letter E

 

Example

10 LET A$ = “123456789”

20 LET B = VAL (A$)

30 PRINT B

The output will be 123456789

 

 

7) STRING FUNCTION (STR$) 

The STR$ function converts number to string. This is the reverse operation of VAL. The value of B is converted into a string literal. B can either be a numeric constant, variable or expression.

Format

STR$ (B)

Where

B is a numeric expression.

 

Example

10 LET B=”123456789″

20 LET A$ = STR$ (B)

30 PRINT A$

 

This will output the string literal 123456789

 

 

8) LENGTH FUNCTION (LEN)

The LEN function returns the number of characters in a string.

Format

LEN (A$)

Where

A$ is a string variable. It may be several concentrated strings and / or variables. The space in a statement is counted as a character.

 

Example 

10 LET A$ = “DON STEVE BLOG”

20 PRINT LEN (A$)

 

The output will be 14

 

 

9) DATE FUNCTION (DAT$)

The DAT$ function returns the current system data.

Format

DAT$ (A)

Where

A is a dummy variable in this function.

 

The date is returned as a ten – character string of the form:

MM/DD/YYYY

Where mm represents month,

dd represents day

yy represents year

 

Example

Study the program segment below:

VR$ = “DON STEVE BLOG”

A = LEN (VR$)

B$ = LEFT$ (VR$, 7)

C$ = RIGHT (VR$, 7)

D$ = MID$ (VR$, 5, 3)

E$ = DAT$

What are the values stored in variable A, B$, C$, D$, ES.

 

Solution

The values stored in the variables are:

A = 14

B$ = DON STE

C$ = VE BLOG

D$ = STE

E$ = 12/25/2022

 

 

10) POSITION FUNCTION (POS)

The POS function returns the location of a specified group of characters in a string.

Format

POS (A$, B$, C)

Where

A$ is the string to be searched. It may be string variable or string constant.

B$ is the series of character been searched for. It may be a string variable or string constant.

C is the position in the string at which the search will begins.

 

The function searches A$ for the first occurrence of B$. The search begins with the Cth character in A$. C may not be less than zero or greater than the length of A$. IF B$ contains no character, the function returns a one. If A$ contains no character, it returns zero. If B$ is not found, it returns a zero.

 

Example

10 DIM D4$ (12)

20 D4$ =”ABCDEFGHIDEF”

30 PRINT POS (D4$, “DEF”,7)

 

The output display will be 10

 

 

11) SEGMENT FUNCTION (SEG$)

The SEG$ function returns the sequence of characters between two positions in a string.

Format

SEG$ (A$, B, C) 

Where

A$ is the string containing characters to be returned. It may be a string variable or string constant.

B is the position of the first character to be returned.

C is the position of the last character to be returned.

 

If B is less than 1, it is set to 1

If B is greater than the length of A$, no characters are returned.

If C is greater than the length of A$, it is set equal to the length of A$.

If C is smaller than B, no characters are returned.

 

Example

10 DIM B2$ (12)

20 B2$ =”ABCDEFGH1DEF”

30 PRINT SEG$ (B2S, 3, 5)

The output will be CDE.

 

Example

Write a program to READ from a DATA statement the alphabet. Your program should the print the following using the correct string functions.

  • The entire alphabet
  • The first eight characters of the alphabet
  • The last twelve characters of the alphabet
  •  The fourteen character of the alphabet

 

Solution

 

10 REM …. PROGRAM TO RUN THE STRING FUNCTION

20 READ A$

30 DATA “A B C D E F G H I J K L M N O P Q R S T U V W X Y Z”

40 REM ….”PRINT ENTIRE ALPHABET”

50 PRINT A$

60 REM …. “PRINT FIRST EIGHT ALPHABETS”

70 PRINT LEFT$ (A$,8)

80 REM …. “PRINT LAST TWELVE ALPHABETS”

90 PRINT RIGHT$ (A$, 12)

100 REM …. “PRINT THE TENTH ALPHABET”

110 PRINT MID$ (A$, 10, 1)

120 END

Leave a Reply

Your email address will not be published. Required fields are marked *

Advantages of local domestic helper. Experience the power of this link building network and watch as your website soars to new heights in the digital landscape.
Blogarama - Blog Directory