String
String
• String are used to stored text.
• String contain a number of characters and the characters in a string can be labelled by position number.
• The first character in a string can be in position zero or position one
Pseudocode
DECLARE Str : STRING
Str “ Hello World”
1 2 3 4 5 6 7 8 9 10 11
H e l l o W o r l d
Library Routines/ Subprogram for String manipulation
• length – finding the number of characters in the string.
Str “Hello World”
Len LENGTH(Str) // 11
• substring- extracting the part of string
Substr SUBSTRING(Str, 1,5) // Hello
Substr SUBSTRING(Str, 7,5) // World
• upper – converting all the letters in a string to uppercase
Str UCASE(Str) // HELLO WORLD
• lower- converting all the letters in a string to lowercase
Str LCASE ( Str) // hello world
(a) Show the value of the variable after each pseudocode statement has been executed.
1. P ← "Computer Science" ........................................................................................
2. Q ← LENGTH(P) ...................................................................................................
3. R ← SUBSTRING(P,10,7) ......................................................................................
4. S ← LENGTH(R) ....................................................................................................
5. T ← SUBSTRING(R,1,3) .......................................................................................
1.(a) Write the pseudocode statements to:
• store the string "The beginning is the most important part" in Phrase
• calculate and output the length of the string
• output the string in upper case.
Phrase “The beginning is the most important part”
OUTPUT LENGTH(Phrase)
OUTPUT UCASE ( Phrase)
(b) Write the output your pseudocode should produce.
40
THE BEGINNING IS THE MOST IMPORTANT PART
2.The variables X, Y and Z are used to store data in a program:
• X stores a string
• Y stores a position in the string (e.g. 2)
• Z stores the number of characters in the string.
(a) Write pseudocode statements to declare the variables X, Y and Z.
DECLARE X : STRING
DECLARE Y : INTEGER
DECLARE Z : INTEGER
(b)The function Length(X) finds the length of a string X.
The function SubString(X,Y,Z) finds a substring of X starting at position Y and Z characters long. The first
character in X is in position 1.
• Write pseudocode statements to:
• store the string "Programming is fun" in X
• find the length of the string and output it
• extract the word “fun” from the string and output it. // SUBSTRING (X, 16,3)
X “Programming is fun”
OUTPUT LENGTH(X)
Y 16
Z 3
OUTPUT SUBSTRING(X,Y,Z)
Pcount 0
FOR Count 1 TO LENGTH(X)
IF SUBSTRING(X,Count,1) = “P” OR SUBSTRING(X,Count,1) = “p” THEN
Pcount Pcount +1
ENDIF
NEXT Count
OUTPUT Pcount
The variables P and Q are used to store data in a program. P stores a string. Q stores a character.
(a) Write pseudocode statements to declare the variables P and Q, store "The world" in P and store
'W' in Q
(b) Write a pseudocode algorithm to:
• convert P to upper case
• find the position of Q in the string P (the first character in this string is in position 1)
• store the position of Q in the variable Position
DECLARE P : STRING
DECLARE Q : CHARACTER
P “The world”
Q ‘W’
PUCASE(P)
FOR Count 1 TO LENGTH(P)
IF SUBSTRING(P,Count,1) = Q THEN
Position Count
ENDIF
NEXT Count
OUTOUT Position
• Write a pseudocode which take the string as the input and
• Find and output the reverse string of the given input string
• Input Hello
• Reverse olleH
• OUTPUT “Enter String:”
• INPUT Str
• Rstr “”
• Index LENGTH(Str)
• REPEAT
• Rstr Rstr+ SUBSTRING(Str,Index,1)
• Index Index-1
• UNTIL Index = 0
• OUTPUT Rstr
Library Routines/ Subprogram for String manipulation
• length – finding the number of characters in the string.
• substring- extracting the part of string
• upper – converting all the letters in a string to uppercase
• lower- converting all the letters in a string to lowercase
Length Language
Mystr “Hello World” Pseudocode
StrLength LENGTH(Mystr)
Or
StrLength LENGTH (“Hello World”)
mystr = “Hello World” Python
strlength = len (mystr)
Or
Strlenght = len (“Hello World”)
Library Routines/ Subprogram for String manipulation
Substring – to extract World Language
Mystr “Hello World” Pseudocode
Substr SUBSTRING (Mystr , 7,5)
Or
Substr SUBSTRING (“Hello World”, 7 , 5)
mystr = “Hello World” Python
mystr[ 6 : 11]
Or
“Hello World” [ 6: 11]
upper Language lower Language
Mystr “Hello World” Pseudocode Mystr “Hello World” Pseudocode
Substr UCASE (Mystr ) Substr LCASE (Mystr)
Or Or
Substr UCASE (“Hello World”) Substr LCASE (“Hello World”)
mystr = “Hello World” Python mystr = “Hello World” Python
substr = mystr.upper() substr = mystr.lower()
Or Or
substr =“Hello World”.upper() substr =“Hello World”.lower()
File Handling
Purpose of storing data in a file
• While any data stored in RAM will be lost when the computer in switched off, when data is saved to
a file it is stored permanently.
• Data stored in a file can be accessed by the same program or accessed by another program.
Write File in Pseudocode Read File in Pseudocode
OPEN filename FOR WRITE OPEN filename FOR READ
WRITEFILE Filename , Data value READFILE Filename , variable name
CLOSEFILE filename CLOSEFILE filename
• Write a pseudocode which take name as the input and store this input
name in the Name.txt file
OUTPUT “Enter Name:”
INPUT Name
OPEN “Name.txt” FOR WRITE
WRITEFILE “Name.txt”, Name
CLOSEFILE “Name.txt”
• Write a pseudocode which read the data in “Data.txt” File and output
the upper case of this data .