String and File
String and File
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
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
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 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]