142k views
3 votes
Write a function named delete Letter that has 2 parameters. The first parameter is a string, the second parameter is an integer. The function removes the character located at the position specified by the integer parameter from the string. For example, if the function is being called with the following statement: deleteLetter ("timetable", 3); the string will become "tietable", where the third character, 'm' has been removed.

User Buena
by
8.1k points

1 Answer

3 votes

Answer:

//import package

import java.util.*;

// class name

class Solution

{

// method to remove the character at given index

public static void deleteLetter(String st,int in)

{

// remove the character at index in

st=st.substring(0,in-1)+st.substring(in);

System.out.println(st);

}

// main method of class

public static void main (String[] args) throws java.lang.Exception

{

try{

//scanner object to read input

Scanner scr=new Scanner(System.in);

// read string

System.out.print("Enter the string:");

String st=scr.nextLine();

// read index

System.out.print("Enter the index:");

int in=scr.nextInt();

// call method

deleteLetter(st,in);

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Read a string with the help of Scanner object.Then read the index of character to be remove.Call the method deleteLetter() with parameter string and index.It will make substring from 0 to "in-1" and "in" to last of the string.Then add both the string.This will be the required string.

Output:

Enter the string:timetable

Enter the index:3

tietable

User Homewrecker
by
8.6k points