630 likes | 762 Views
Structured Problem Solving 2009-2010. Week 10: Java: Strings Stewart Blakeway FML 213 blakews@hope.ac.uk. What we have done already. Seen what an algorithm is a set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms in
E N D
Structured Problem Solving2009-2010 Week 10: Java: Strings Stewart Blakeway FML 213 blakews@hope.ac.uk
What we have done already • Seen what an algorithm is • a set of instructions that, if carried out, will lead to a successful conclusion • Learned how to represent algorithms in • Structured English • Flow charts • Used variables to remember
What we have done already • Applied the top down, stepwise refinement approach to creating algorithms • Looked at problems more oriented towards being solved on a computer – stacks, queues, functions, procedures • Seen how high level languages like Java are used to create programs with the aide of compilers • The problem solving constructs in Java • Nested for loops
What we shall do today • Strings • 3 examples • Reversing the order of characters in a word • Counting the number of words in a sentence • Analysing a URL (e.g. www.hope.ac.uk)
Strings in computing • Let Σ be an alphabet, a non-empty finite set. • Elements of Σ are called symbols or characters. • A string (or word) over Σ is any finite sequence of characters from Σ. • Reference: “http://en.wikipedia.org/wiki/String_(computer_science)”
Example • Suppose Σ = { W, $, Þ,Ŷ, Ǿ}. • Which of the following are valid strings over this alphabet: • ǾW$$ • WORD • Þ • $Ŷw Yes No – ‘O’, ‘R’, ‘D’ not in given alphabet Yes No – ‘w’ not in given alphabet
Strings in Java • Alphabet includes all ASCII characters • ‘A’..’Z’ • ‘a’..’z’ • ‘0’..’9’ • Punctuation marks • Certain “unprintable” characters like TAB, NEWLINE are represented by ‘\t’, ‘\n’ • Works next term in BlueJ – not here in Java Trainer
Strings in Java • Alphabet Σ = {‘A’..’Z’, ‘a’..’z’, ‘0’..’9’, …..}
Java Strings String s; s = "Fred"; System.out.println(s);
Concatenating strings Concatenate: Etymology: Middle English, from Late Latin concatenatus, past participle of concatenare to link together, from Latin com- + catena chain Date: 15th century
Concatenating strings with + String a; String b; String c; b = "Fred"; c = "Bloggs"; a = b + c; System.out.println(a);
Accessing characters in Strings String s; s = "Fred"; s[0]is'F' s[1]is'r' s[2]is'e' s[3]is'd'
Accessing characters in Strings String s; s = "Fred"; System.out.println(s[0]);
Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.println(s[i]); }
Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.println(s[i]); }
Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.print(s[i]); }
Accessing characters in Strings String s; int i; s = "Fred"; for (i=0; i<4; i++) { System.out.print(s[i]); }
Write out a word in reverse String s; int i; s = "Fred"; for (i=3; i>=0; i--) { System.out.print(s[i]); }
Reversing a word of any length String s; int i; System.in.read(s); for (i=3; i>=0; i--) { System.out.print(s[i]); }
We need to be able to find the length of the string • Java Trainer has a FUNCTION that returns the length of a String • It is called length String s; System.in.read(s); int n; n = length(s);
Reversing a name of any length String s; int i; int n; System.in.read(s); n = length(s); for (i=n-1; i>=0; i--) { System.out.print(s[i]); }
Initial statement • Count the number of words in a sentence
Initial statement • Count the number of words in a sentence by counting spaces then adding 1
Initial statement • Count the number of words in a sentence by counting spaces then adding 1 • Assume single space between words • Assume no spaces at beginning or end of sentence
First refinement Get sentence Initialise count Count the spaces Report the final count + 1
Second refinement Read Sentence Count := 0 While (not reached end of sentence) begin deal with next character end Display Count + 1
Second refinement • Do we know before we start how many times we shall go round the loop? • Yes – since we can use the length function to get the number of characters. • So, we can use a for loop
Second refinement Read Sentence Count := 0 For each character in the sentence begin deal with the character end Display Count + 1
Third refinement Read Sentence Count := 0 For each character in the sentence begin if (character = '') begin Count = Count + 1 end end Display Count + 1
Counting the number of words in a sentence: in Java String sentence; int count; inti; count = 0; System.in.read(sentence); for (i=0; i<length(sentence); i++) { if (sentence[i] == ' ') { count = count + 1; } } System.out.println("There are " + (count+1) + " words.");
Analysing a URL • URL -> Universal Resource Locator • URL is the basis for communicating locations of resources (data) on the web. • E.g. http://www.hope.ac.uk • A URL consists of: • a protocol identifier (e.g. HTTP (hypertext transfer protocol), FTP (file transfer protocol) • and a protocol-specific syntax further defining the location.
Analysing a URL • In this example we shall analyse a HTTP protocol URL to extract information about the location of the host. • E.g. http://www.hope.ac.uk
Analysing a URL • In this example we shall analyse a HTTP protocol URL to extract information about the location of the host. • E.g. http://www.hope.ac.uk Host name
Analysing a URL • http://www.hope.ac.uk Country
Analysing a URL • http://www.hope.ac.uk Classification
Analysing a URL • http://www.hope.ac.uk Organisation
Analysing a URL • http://www.hope.ac.uk Service
Analysing a URL • http://www.hope.ac.uk • Country = uk (United Kingdom) • Classification = ac (Academic) • Organisation = hope (Liverpool Hope) • Service = World Wide Web (Hope’s web server)
Analysing a URL • http://moodle.hope.ac.uk • Country = uk (United Kingdom) • Classification = ac (Academic) • Organisation = hope (Liverpool Hope) • Service = moodle (Hope’s VLE)
Analysing a URL • Country • uk (United Kingdom) • de (Germany) • fr (France) • Classification • ac (academic) • gov (government) • org (organisation)
Analysing a URL – .COM is different • http://www.microsoft.com USA (or an organisation based anywhere)
Analysing a URL – .COM is different • http://www.microsoft.com Organisation
Analysing a URL – .COM is different • http://www.microsoft.com Service
Analysing a URL • http://www.microsoft.com • Organisation based anywhere • Organisation = microsoft • Service = world wide web
Initial statement Analyse a URL