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...

  • Write a C++ program that takes a string containing a full name in lower case letters...

    Write a C++ program that takes a string containing a full name in lower case letters and outputs each part of the name separately with initial letters capitalized. The name should be in the form of first, middle, and last name, separated from each other by a single space. For example, if the name string contains "heidi kim lee" then the program would output First name: Heidi Middle name: Kim Last name: Lee

  • 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...

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • 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...

  • In Java,enhace the program base on the first one. 1. You are going to design (and...

    In Java,enhace the program base on the first one. 1. You are going to design (and code) a class called Name. The class Name will contain three properties: first, the first name which is a String initial, the middle initial, which is a single character. last, the last name, which is a String The class has three constructors: A default constructor, which accepts no parameters, calls constructors for the first and last name and sets the initial equal to a...

  • 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.

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