Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Text Processing; Learn how to use String Methods

phillyluvr
phillyluvr 


Lab 9 Text Processing Objectives Learn how to use String methods Key Concepts 􀁸 The String class provides several methods for working with String objects. Lab Assignment This lab assignment will ask you to write a program prints a String object backward and counts the number of words it contains. Exercise 1 1. Create a New Project named StringDemo 2. Write a method called “backward()” that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is “gravity” the method should display -“ytivarg”. Demonstrate the method in a program that asks the user to input a string and then passes it to the method. 3. Write a method called “wordCount()” that accepts a String object as an argument and returns the number of words it contains. In this case, we will assume blank spaces separate words. For instance, if the argument is “Four score and seven years ago” the method should return the number 6. Demonstrate the method in a program that asks the user to input a string and then passes it to the method. The number of words in the string should be displayed on the screen. (Hint: the split() String method could be useful.) Attachments Tags Clarifications

Answers

Solution for StringDemo

mariobeka
mariobeka 



Below you will find the solution for the StringDemo. Attachments
StringDemo.java (1 K) Download Preview
{ // Initializes the string demo. StringDemo sd = new StringDemo(); String demo = "gravity"; System.out.println("The string "+ demo+" backwards is "+sd.backward(demo)); Scanner sc = new Scanner(System.in); System.out.println("Give me a string:"); String string = sc.nextLine(); System.out.println("There are " + sd.wordCount(string) + " words

Screen Shot 2015-10-28 at 19.25.52.png (24 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Create a New Project named StringDemo

felo
felo 



StringDemo Attachments
StringDemo.rar (3 K) Download





Preview
String original, backward = ""; Scanner in = new Scanner(System.in); System.out.println("Enter a string to reverse"); original = in.nextLine();

Preview
args) throws IOException { // Scanner for keyboard input Scanner keyboard = new Scanner(System.in); System.out.print("Give me a string to count the words: "); // Get input from user String input = keyboard.nextLine();

GRAVITYOUT.png (162 K) Download Preview

GRAV.png (164 K) Download Preview

Screenshots
Screenshot
Screenshot
Purchase Answer