Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Post Top Ad

Your Ad Spot

Wednesday, February 16, 2022

String library functions in QBASIC

 

String Library Functions


Introduction

In programming, string manipulation is an important skill for efficiently managing text data. QBASIC, an early but enduringly popular programming language, includes several built-in string handling functions. Understanding string library functions is critical whether you're a seasoned developer returning to QBASIC or a newcomer eager to learn about its features. In this guide, we'll look at the core string functions available in QBASIC and provide practical examples and tips for using them in your code.

What are the String Library Functions in QBASIC?

QBASIC's string library functions are a set of built-in tools for performing various string operations. These functions enable programmers to effectively manipulate, search, and format text. Understanding these functions can improve your ability to work with text-based data, ranging from simple string concatenation to complex pattern searching.

 

LEN

This function returns the length of a string expression including a blank space.

Syntax: LEN(string expression)

OR

LEN(variable)

Where,

String expression is a string constant

Variable is a string variable

 Example:

REM Example LEN function

CLS

PRINT LEN("QBASIC")

a$ = "PROGRAMMING"

l = LEN(a$)

PRINT l

END

Output



 

 

LEFT$

This function allows us to extract the specific number of characters beginning from the left-most character of the string.

Syntax: LEFT$(string expression, n)

Where,

String expression is a string constant or string variable.

n is the specifies number of characters to be extracted from left- hand side. (Ranges from 0-32767)

 Example:

REM Example of LEFT$ function.

CLS

PRINT LEFT$("QBASIC", 2)

a$ = "PROGRAMMING"

PRINT LEFT$(a$, 4)

END

Output



 

 

REGHT$

This function allows us to extract the specific number of characters beginning from the right-most character of the string.

Syntax: RIGHT$(string expression, n)

Where,

String expression is a string constant or string variable.

n is the specifies number of characters to be extracted from right-hand side. (Ranges from 0-32767)

 Example:

REM Example of LEFT$ function.

CLS

PRINT RIGHT$("QBASIC", 5)

a$ = "PROGRAMMING"

PRINT RIGHT$(a$, 6)

END

Output



 

 

MID$

This function is used to returns a specific number of characters from a string.

Syntax: MID$(string expression, start, length)

Where,

String expression is the string constant or variable.

Start is the position measured from the lest-most character.

Length is the number of characters to extract.

 Example:

REM Example of LEFT$ function.

CLS

PRINT MID$("QBASIC", 2, 3)

a$ = "PROGRAMMING"

PRINT MID$(a$, 4, 4)

END

Output



 

 

LCASE$

This function converts given uppercase string in lowercase (small letter).

Syntax: LCASE$(string expression)

Where,

String expression can be a string constant or string variable.

Example:

REM Example of LCASE$ function.

CLS

PRINT LCASE$(“AVN”)

END

Output

 

avn

 

 

 

UCASE$

This function converts given lowercase string into uppercase (capital letter)

Syntax: UCASE$(string expression)

Where,

String expression can be a string constant or string variable.

Example:

REM Example of UCASE$ function.

CLS

PRINT UCASE$(“nepal”)

END

Output

 

NEPAL

 

 

 ASC

This function returns ASCII value of the first character of the given string.

ASCII: American Standard Code for Information Interchange

Following are the some common ASCII codes


ASCII codes

Characters

65 to 90

A to Z in capital letters

97 to 122

a to z in small letters

48 to 57

0 to 9 numbers

0 to 31

Control characters

32 to 64

Punctuation and mathematical symbols

91 to 96 and 123 to 127

Miscellaneous symbols

  Syntax: ASC(string expression)

Where,

String expression can be string constant or string variable.

Example:

REM Example of ASC function.

CLS

PRINT ASC(“AVN”)

PRINT ASC (“a”)

END

Output

 

65

97

 

 CHR$

This function is used to converts ASCII code into character.

Syntax: CHR$(numeric expression)

Where,

Numeric expression is any number of variable within the  character code range from 0 to 255.

Example:

REM Example of CHR$ function.

CLS

PRINT CHR$(66)

PRINT CHR$(98)

PRINT CHR$(2)

END

 Output



 

 

STRING$

This function is used to return a string of specified length by repeating the particular characters whose ASCII code or first character of string expression is given.

Syntax: STRING$(length, ASCII code/string expression)

Where,

Length is a numeric expression to print a character for a specified number of times.

ASCII code can be used to display an equivalent character.

String expression is the character to be displayed.

Example:

REM Example of STRING$ function.

CLS

PRINT STRING$(5, "AVN")

PRINT STRING$(10, "=")

END

Output



 

 VAL

This function is used to converts a string expression containing of digit into numeric value. After converting, the value comes in numeric nature and can be used for mathematical calculations.

Syntax: VAL (string expression)

Where,

String expression can be a string constant or string variable.

Example:

REM Example of VAL function

CLS

a$ = "47AVN23"

b = 15

c = b + VAL(a$)

PRINT c

END

Output

 

 

62

 Note: In the above program, the value of a$ is “47AVN23”. VAL function translates only 47 to the numeric and store in variable v, because 23 appears after letters AVN, which is not translated.

 

STR$

This function is used to returns the string equivalent of the ASCII value of numeric expression.

Syntax: STR$(numeric expression)

Where,

Numeric expression can be any kind of number.

Example:

REM example of STR$ function.

CLS

a = 10

b$ = "Ten"

a$ = STR$(a)

c$ = b$ + a$

PRINT c$

END

Output

 

 

Ten  10

 Note: In the above program, the value of numeric variable a is 10. The function STR$(a) changes the numeric value of variable a to string and store in the string variable a$.

 

SPACE$

This function is used to generate a string of space of a specified number of character length.

Syntax:  SPACE$(n)
Where,

n is the number of characters.

Example:

REM Example of SPACE$ function.

CLS

a$ = SPACE$(10)

b$ = STRING$(5, "*")

PRINT b$; a$; b$

END

 

Output





 

 

OCT$

This function returns a string representing the octal value of the numeric argument.

Syntax:  OCT$(numeric expression)

Example:

 

PRINT OCT$(30)

Output

 

36

 

HEX$

This function returns a string representing the hexadecimal value of the numeric argument.

Syntax: HEX$(numeric expression)

Example:

 

PRINT HEX$(30)

Output

 

1E

 

 

INKEY$

It is an I/O function that returns a one or two byte string containing a character read from the keyboard. A null string is returned if no character is waiting there. A one character string containing the actual character read from keyboard that reads a character from the keyboard.

Syntax:  INKEY$

Example:

REM Example of INKEY$ function

CLS

PRINT "Press any key......."

DO

LOOP WHILE INKEY$ = ""

END

Output



 Note: This program pause until the user presses a key.


Conclusion


Mastering string library functions in QBASIC allows you to handle text data with greater precision and efficiency. You can perform a wide range of string manipulations using functions such as LEN, MID, LEFT, RIGHT, INSTR, STR$, LTRIM$, RTRIM$, UCASE$, and LCASE$, which are essential for any text-based application.

These string functions will be extremely useful whether you're working on a simple program or a more complex project. Continue to experiment with these tools to fully realize the power of QBASIC's string manipulation capabilities.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages