Test Your Knowledge: Exercises

Exercise 15-1. Create the following six strings:

  • String 1: “Hello "

  • String 2: “World”

  • String 3 (a verbatim string): “Come visit us at http://www.LibertyAssociates.com"

  • String 4: a concatenation of strings 1 and 2

  • String 5: “world”

  • String 6: a copy of string 3

Once you have the strings created, do the following:

  1. Output the length of each string.

  2. Output the third character in each string.

  3. Output whether the character H appears in each string.

  4. Output which strings are the same as string 2.

  5. Output which strings are the same as string 2, ignoring case.

Exercise 15-2. Take the following famous string:

To be, or not to be: That is the question: Whether ‘tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by opposing end them?

Reverse the order of the words in the string, and output the reversed string to the console.

Exercise 15-3. Take the following famous string:

We choose to go to the moon. We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win, and the others, too.

Write a program to determine and output to the screen the number of times the word the occurs in the string.

Exercise 15-4. Take the following string:

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.

and use a regular expression to split the string into words. Then create a new string that lists each word, one to a line, each prefaced with a line number.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset