Download presentation
Presentation is loading. Please wait.
Published byMagnus Cole Modified over 8 years ago
1
String Manipulation
2
LENGTH(s) Returns the length(no. of char., including spaces) of string s E.g. S:=‘Computer Studies’; Writeln(Length(s)); Output : 16
3
String Concatenation ( + ) Returns the string by joining all the strings together in order May use concat(s1,s2,s3) E.g. a:=‘Apple’; b:=‘Orange’; C:=a+b+a; Writeln(c+b); Output : AppleOrangeAppleOrange
4
ORD(c) Returns the ASCII value of character c E.g. C:=‘J’; A:=ord(c); Writeln(a); Output : 75
5
CHR(x) Returns the character with ASCII value x May be used to issue control command(non printable characters) E.g. C:=chr(90); Writeln(c); Writeln(ord(c)); Output: Z 90
6
Copy(s,i,n) Returns the substring extracted from string s, starting at position i, and of length n E.g. S:=‘computer studies’; T:=copy(s,1,3)+copy(s,15,2); Writeln(t); Output : comes
7
str(n,s) procedure Procedure that converts the number n into string s May be: str(n : format, s) e.g. Str(123.456:7:2,s); Writeln(‘string converted=‘,s); Output : String converted=_123.46
8
Val(s,n,e) procedure Procedure to convert string s(in numeric format) into number n, with error code e If s not in numeric format, e will store the position of the first character which goes wrong, otherwise zero If conversion failed, n will be zero
9
VAL(s,n,e) (– cont’d) E.g. Val(‘123.45’,n1,e1) Val(‘12A.45’,n2,e2) If e1=0 then writeln(‘no. converted=‘,n1) else Writeln(‘error occur at position ‘,e1); If e2=0 then writeln(‘no. converted=‘,n2) else Writeln(‘error occurs at position ‘,e2); Output :no. converted=123.45 error occurs at position 3
10
Be Careful of: Is it a procedure or function ? How many arguments ? What is the type of the arguments ? What is the order of the arguments ?
11
Exercises 1. Write a procedure to convert a sentence to uppercase(lowercase letters only, others unchanged). E.g. upper(sentence) ‘Mr. Chan’ becomes ‘MR. CHAN’ 2. Write a procedure to convert a message to secret code with ‘A’ ‘E’, ‘B’ ‘F’, …, ‘V’ ‘Z’, ‘W’ ‘A’, ‘X’ ‘B’, ‘Y’ ’C’, ‘Z’ ’D’ ; with all other characters unchanged. Note : Change to uppercase and initialize secret to string of spaces the same length as message. e.g. Encode(message,secret) ‘BIN LADEN is alive.’ becomes ‘FMR PEHIR MW EPMZI.’
12
Exercises 3. Write a procedure to recover a secret code back to the original message with the coding scheme in (2) above. E.g. decode(secret,message); ‘FMR PEHIR MW EPMZI’ becomes ‘ BIN LADEN is alive’ 4. Write a procedure to search a target string within a source string. Return the position of the first substring that matches the target string and a boolean value stored in success which indicates that the target string is found or not. E.g. search(source,target,p,success)
13
Exercises 5. Write a procedure to search a target string within a source string starting from a certain position. Return the position found and the boolean variable success. E.g. search(source,target,start,p,success) 6. Find the frequencies of occurrences of each letter from ‘A’ to ‘Z’ in a sentence. Save the counts in an array F[‘A’..’Z’] of integer. E.g. sentence = ‘I am a student.’ F[‘A’]=2, F[‘D’]=1, F[‘E’]=1, F[‘I’]=1, F[‘M’]=1, F[‘N’]=1,F[‘S’]=1, F[‘T’]=2, F[‘U’]=1, …others =0
14
Exercises 7. Write a procedure to insert a middle string in a source string starting at position p. E.g. Middle:=‘clever ’; P:=3; ‘source:=‘a boy’; Insert(source,middle,p) Source will become ‘a clever boy’ 8. Write a procedure to replace a target string in a source string with a new string. E.g. source=‘a stupid boy’; Target=‘stupid’; New:=‘foolish’; replace(source,target,new,success) Source will become ‘a foolish boy’
15
END
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.