Question



String Manipulator Write a program to manipulate Strings. Store your full name into one String variable. It must include firs
use Java and it must work for any name
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//save it as stringManipulator.java

package project123;
import java.util.Scanner;

public class stringManipulator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String fullName;
System.out.print("Enter full name: ");
fullName = sc.nextLine();
fullName = fullName.trim(); //remove extra spaces at the start and end of string
  
run(fullName);
}
public static int numChars(String nameString) {
int count = 0;
for (int i = 0; i < nameString.length(); i++) {
//read each char from name and count characters excluding space and tab
if (nameString.charAt(i) != ' ' && nameString.charAt(i) != '\t') {
//if char is not a space and not a tab then
count++;
}
}
return count;
}
public static int numChars(String first, String middle, String last) {
int n;
// make full name by trimming start and end spaces
String full = first.trim()+ " " + middle.trim() + " " + last.trim();
System.out.print(full + " ");
n = numChars(full); //call our existing method to count characters
return n;
}
  
public static String getFName(String nameString) {
String fname;
nameString = removeSpaces(nameString); //get clean name without extra spaces and tabs
String words[] = nameString.split(" ");//split nameString into words array by space
//so that first element in array has first word in string i.e firstname
  
int i = 0; //first element of array
fname = words[i]; //first word in array is firstname
return fname;
}
  
public static String getLName(String nameString) {
String lname;
nameString = removeSpaces(nameString); //get clean name without extra spaces and tabs
String words[] = nameString.split(" ");//split nameString into words array by space
//so that last element in array has last word in string i.e lastname
  
int i = words.length-1; //last element of array
lname = words[i]; //last word in array is lastname
return lname;
}
  
public static String getMName(String nameString) {
String middle="";
String fullName = removeSpaces(nameString);
String words[] = fullName.split(" ");
if (words.length == 3) { //if fullname contains 3 words then proceed to get middle
middle = words[1]; //get second word as middle name
}
else{ //if fullname doesn't has 3 words then, cannot get middle name
System.out.println("Cannot get middle name from: " + fullName);
return "";
}
return middle;
}
  
public static String removeSpaces(String nameString) {
//get clean name without extra spaces and tabs
String name="";
nameString = nameString.replace("\t", ""); // remove tabs from string
String words[] = nameString.split(" ");//split nameString into words array by space
  
//now create name using extracting nonEmpty words from above array
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 0) { //if word is not empty
name += " " + words[i]; //then include it in name
}
}
return name.trim();
}
  
public static int indexLastName(String nameString) {
String name = removeSpaces(nameString); //get clean name without extra spaces and tabs
String lastName = getLName(name);
int index = name.indexOf(lastName);
  
return index;
}
  
public static String encryptFName(String fname) {
String str = "";
  
if (fname.length() > 0) {
char fchar = fname.charAt(0); //first character
char lchar = fname.charAt(fname.length()-1); //last character
  
str += lchar; // add last char at begining
for (int i = 1; i < fname.length()-1; i++) {
//add all chars to string except fist and last
str += fname.charAt(i);
}
str += fchar; //add first char at the end
}
return str;
}
public static String getUpperName(String nameString) {
nameString = removeSpaces(nameString); //get clean name without extra spaces and tabs
  
return nameString.toUpperCase();
}
  
public static String getReverse(String nameString) {
nameString = removeSpaces(nameString); //get clean name without extra spaces and tabs
String reverse="";
for(int i = nameString.length() - 1; i >= 0; i--)
{
reverse = reverse + nameString.charAt(i);
}
return reverse;
}
  
public static void run(String fullName) {
System.out.print("1. Number of characters in my complete name are: ");
System.out.println( numChars(fullName) );
System.out.print("2. First name is: ");
System.out.println( getFName(fullName) );
System.out.print("3. Last name is: ");
System.out.println( getLName(fullName) );
System.out.print("4. Middle name is: ");
System.out.println( getMName(fullName) );
  
System.out.println("5. passing first middle last names separatly");
System.out.print(" Number of characters in my full name are: ");
System.out.println( numChars("John", "Plain", "Doe")); //pass first middle last names separatly
  
System.out.print("6. Index of last name is: ");
System.out.print( indexLastName(fullName) );
System.out.println(", Full Name= " + removeSpaces(fullName));
System.out.print("7. encrypted First name is: ");
String encrp = encryptFName( getFName(fullName) ); //pass fname to encrypt method
System.out.println( encrp );
System.out.print("8. decrypted First name is: ");
String decrp = encryptFName(encrp); //if we pass encrypted fname to method it will swap first and last chars,
//so we get decrypted fname
System.out.println( decrp );
System.out.print("9. UpperCase name is: ");
String upper = getUpperName(fullName);
System.out.println( upper );
System.out.print("10. Reverse name is: ");
String rev = getReverse(fullName);
System.out.println( rev );
}
}


/* output
Enter full name: John Plain Doe
1. Number of characters in my complete name are: 12
2. First name is: John
3. Last name is: Doe
4. Middle name is: Plain
5. passing first middle last names separatly
Number of characters in my full name are: John Plain Doe 12
6. Index of last name is: 11, Full Name= John Plain Doe
7. encrypted First name is: nohJ
8. decrypted First name is: John
9. UpperCase name is: JOHN PLAIN DOE
10. Reverse name is: eoD nialP nhoJ

*/

Add a comment
Know the answer?
Add Answer to:
use Java and it must work for any name String Manipulator Write a program to manipulate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string...

    JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string of someone's full name and prints it out last name first. You may use the available string manipulation methods. Example: "George Washington" "Washington, George" 2. Write a program that will take a string of someone's full name, and break it into separate strings of first name and last name, as well as capitalize the first letter of each. Example: "joseph smith", "Joseph" "Smith"

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write a program in java to read a string object consisting 300 characters or more using...

    Write a program in java to read a string object consisting 300 characters or more using index input Stream reader. The program should perform following operations. The String must have proper words and all kind of characters.(Use String class methods). a, Determine the length of the string count the number of letters in the strings , count the number of numeric object d. Calculate the number of special character e. Compute the ratio of the numeric to the total f....

  • Write a program that separately prompts the user for a first name and last name and...

    Write a program that separately prompts the user for a first name and last name and outputs a string containing the following information, in order: a. First letter of the user's name. b. First five letters of the user's last name. c. A random two-digit integer You must construct the desired string ensuring all characters are lowercase; output the identification string accordingly. Assume the last name contains at least 5 characters. You must use the Random (java.util.Random) class to generate...

  • Must be in JAVA. Write a program that uses a Scanner to read in a String....

    Must be in JAVA. Write a program that uses a Scanner to read in a String. The program will then output a new String with all the vowels (upper and lower case) removed. See output for example output. Details Input A string composed of non-numeric characters Output The input string with the vowels removed Sample input: Welcome to Dalhousie Sample output: Dlhs nvrsty

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

  • Background information: : Implement your toString method. The output should be in the format ik Name:...

    Background information: : Implement your toString method. The output should be in the format ik Name: (Phone Number: ) *For example, if a contact has the first name John, last name Doe and middle name Xavier, and the phone number 123-456-7890, this should return: ik Name: Doe, John Xavier (Phone Number: 123-456-7890) *Do not insert new line character after the last digit of the phone number ik public String toStringO t

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in...

    USE JAVA PLEASE And answer all questions Question 5.1 1. Write a program that reads in a number n and then reads n strings. it then reads a string s from the user and gives you the word that was typed in after s. Example input: 4 Joe Steve John Mike Steve Example output: John Example input: 7 Up Down Left Right North South East Right Example output: North 2. Remember to call your class Program Question 5.2 1. Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT