IT145 - 3.8 String access operations.doc - 3.8 String access operations A string is a sequence of characters in memory Each string character has a

IT145 - 3.8 String access operations.doc - 3.8 String...

This preview shows page 1 - 3 out of 8 pages.

3.8 String access operationsA string is a sequence of characters in memory. Each string character has a position number called an index. The numbering starts with 0, not 1.charAt(): The notation someString.charAt(0) determines the character at a particular index of a string, in this case index 0.Figure 3.8.1: String character access.importjava.util.Scanner;publicclassWordScramble {publicstaticvoid main(String[] args) {Scanner scnr = newScanner(System.in);StringusrWord = "";System.out.print("Enter a word with 5 letters: ");usrWord = scnr.next();System.out.println("Size: " + usrWord.length());// Note: Error if usrWord has < 5 lettersSystem.out.println("Original: " + usrWord);System.out.print("Scrambled: ");System.out.print(usrWord.charAt(3));System.out.print(usrWord.charAt(4));System.out.print(usrWord.charAt(1));System.out.print(usrWord.charAt(0));System.out.println(usrWord.charAt(2));return;}}Enter a word with 5 letters: StarsSize: 5Original: StarsScrambled: rstSaFeedback?PARTICIPATIONACTIVITY3.8.1: String access.Given userText is "Think". Do not type quotes in your answers.1)How many numbers do you see: 0 1 2 3CheckShow answer
Background image
2)What character is at index 1 of userText?CheckShow answer3)What is the index of the last character, 'k', in userText?CheckShow answerCorrect
Background image
Image of page 3

You've reached the end of your free preview.

Want to read all 8 pages?

  • Summer '14
  • 8 , Dang, 2034

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture