1. Write a program that reads a string and counts the number of words in it. Wor
ID: 3675025 • Letter: 1
Question
1. Write a program that reads a string and counts the number of words in it.
Words are dened as substrings separated by spaces. For instance, whenthe user enters “Call me Ishmael” the program should output 3.
2. A palindrome is a word, phrase, number, or other sequence of characterswhich reads the same backward or forward. Write a program that reads astring and outputs it in reverse. For instance if the user enters “abcdef”,your program should output “fedcba”. Now, add the code to print to thescreen whether the original string is a palindrome.
Explanation / Answer
1)
/**The java program that promtps user enter the stirng
* from user and prints the number of words in the string*/
//WordCount.java
import java.util.Scanner;
public class WordCount
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//delcare string variable
String sentence;
System.out.println("Enter a string");
//read string
sentence=scanner.nextLine();
//calling countWords
int count=countWords(sentence);
//print sentence
System.out.println("Input String : "+sentence);
//print count
System.out.println("Number of words : "+count);
}
/**Method countWords that takes string as input
* and returns the number of words in the string sentence*/
private static int countWords(String sentence)
{
int count=1;
//run for loop
for (int i = 0; i < sentence.length(); i++)
{
//Check if character at i is space
if(sentence.charAt(i)==' ')
//increment the count by one
count++;
}
//return count vaoue
return count;
}
}
Sample output:
Enter a string
Call me Ishmael
Input String : Call me Ishmael
Number of words : 3
------------------------------------------------------------------------------------------------------------------------
2)
/**The java program Palindrome that prompts user to enter
* string value and calls reverse method.
* Then checks if the string and its reverse are equal
* then print palindrome otherwise print not palindrome*/
//Palindrome.java
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//delcare string variable
String str="";
System.out.println("Enter a string ");
//read string from user
str=scanner.nextLine();
//Call reverse method with string value, str
String rev=reverse(str);
//Check if str is equal to rev string
if(str.equals(rev))
//print palindrome
System.out.println("Palindrome");
else
//otherwise print not palindrome
System.out.println("Not palindrome");
}
/**The method reverse takes string, str that reverse the
* string and returns reverse of a string*/
private static String reverse(String str)
{
String reverse="";
for (int i = str.length()-1; i>=0 ; i--)
reverse+=str.charAt(i);
//returns reverse of string
return reverse;
}
}//end of class
sample output:
Enter a string
madam
Palindrome
output2:
Enter a string
jasmine
Not palindrome
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.