1 / 12

Handling Strings

Handling Strings. REVIEW: What is a string?. String Operations. There is a predefined function called length(string variable) that will accept a string variable as its arguments and output the length of that string.

faris
Download Presentation

Handling Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Handling Strings REVIEW: What is a string?

  2. String Operations There is a predefined function called length(string variable) that will accept a string variable as its arguments and output the length of that string. It is also possible to join two strings together. This is called catenation. The “+” sign allows you to join two strings. For example: put “O” + “K” will output OK

  3. String1.t % The "WordsOnLine" program % Reads 5 words and outputs all on a line varword : string varline := "" % Initialize line to the empty string put "Enter 5 words" for i : 1 .. 5 get word line := line + word + " " end for put line put “The length of the words is “, length(line)

  4. Selecting part of a string When you select part of a string, this is called a substring. The character position of a string are numbered starting from the left (1) and increase by one as you go to the right. For example: If you had the string “Hello” H would be in the first spot, e would be in the second and so on. The last position can also be identified using an asterisk *

  5. String2.t % The "FinalThreeLetters" program varword : string put "Enter words one to a line, end with 'quit' " loop put "Enter word: " .. get word exit when word = "quit" if length (word ) >= 3 then put word (* – 2 .. *) else put "Word has fewer than 3 characters" end if end loop

  6. String3.t % The "LetterAtATime" program % Read a word and output it a letter-at-a-time varword : string put "Enter words, end with 'tired' " loop put "Enter word: " .. get word exit when word = "tired" for i : 1 .. length (word ) put word (i ) end for end loop put "Why not take a rest?"

  7. Searching for a pattern in a string • We can also search a string to see if it has a pattern and get the location of where that pattern is. • index (string, pattern) For example: index (“habs”, “a”) will return a value of “2” because the character a is in the second position. If there is no match then the index function will return a value of 0.

  8. String4.t % Test to see if a word contains an “r" varword : string put "Enter a series of words, end with 'last' " loop put "Enter word: " .. get word exit when word = "last" if index (word, “r") not= 0 then put word, " contains an ‘r' " else put word, " does not contain an ‘r’ " end if end loop

  9. Bulletproofing programs • It is important that you make sure the user cannot input invalid data to crash your program. The most common error is when the user inputs an alphabetical character for an input instead of a number. Reading all inputs as strings avoid this problem.

  10. String5.t varinput : string varage : int put "Enter your age: " .. loop get input exit when strintok (input ) put "Not a number. Please enter an integer: " .. end loop age := strint (input )

  11. strintok is a program that returns true if can convert a string to an integer • strintis a program that converts a string into an integer.

  12. Exercises • Pg.222/500 • #1,2,6,13

More Related