Question

In: Computer Science

I need to write java program that do replace first, last, and remove nth character that...

I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods.

No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive.

if user's input was "be be be" and replace first 'b'

replace first should do "e be be"

replace last should do "be be e"

remove nth character such as if user want to remove 2nd b

it should be like "be e be"

Solutions

Expert Solution

Solution:

import java.util.Scanner;

public class Replace {
    public static void main(String[] args) {
        String input;
        String first = "", last = "", n = "", x;
        int choice, i, index, count = 0;

        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string:");
        input = in.nextLine();

        while (true) {
            System.out.print("\n1.Replace First\n2.Replace Last\n3.Replace nth\n4.Exit\n");
            System.out.print("Enter your choice:");
            choice = in.nextInt();
            switch (choice) {
                case 1:
                    System.out.print("Enter the character whose first occurrence has to be replaced:");
                    x = in.next();
                    for (i = 0; i < input.length(); i++) {
                        if (input.charAt(i) == x.charAt(0)) {
                            first = input.substring(0, i) + input.substring(i + 1);
                            break;
                        }
                    }
                    System.out.print("Result is " + first);
                    break;
                case 2:
                    System.out.print("Enter the character whose last occurrence has to be replaced:");
                    x = in.next();
                    for (i = input.length() - 1; i >= 0; i--) {
                        if (input.charAt(i) == x.charAt(0)) {
                            last = input.substring(0, i) + input.substring(i + 1);
                            break;
                        }
                    }
                    System.out.print("Result is " + last);
                    break;
                case 3:
                    System.out.print("Enter the character whose nth occurrence has to be replaced:");
                    x = in.next();
                    System.out.print("Enter the value of n:");
                    index = in.nextInt();
                    for (i = 0; i < input.length(); i++) {
                        if (input.charAt(i) == x.charAt(0))
                            count++;
                        if (count == index)
                            break;
                    }
                    n = input.substring(0, i) + input.substring(i + 1);
                    System.out.print("Result is " + n);
                    break;
                case 4:
                    System.out.print("Bye\n");
                    System.exit(0);
            }
        }
    }
}

Output:


Related Solutions

How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
I need know how to Write a java program called character length that prompts the user...
I need know how to Write a java program called character length that prompts the user to enter a string and displays: The length of the string is: xxxxx The strings first character is: x    (where xxxxx is the length of the string and x is the first character.)
In Java, write a program that finds the first character to occur 3 times in a...
In Java, write a program that finds the first character to occur 3 times in a given string? EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT