Exp.
Name: Write a Java program that uses functions to perform the following
S.No: 15 Date: 2022-06-15
ID: 20095A0363
Page No:
operations:
Aim:
Write a Java program that uses functions to perform the following operations:
ii) Deleting n characters from a given position in a given string.
Source Code:
RemoveCharN.java
import java.lang.*;
import java.util.*;
public class RemoveCharN {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String: ");
String str = sc.nextLine();
Scanner sc1 = new Scanner(System.in);
System.out.print("Enter the no. of characters to be deleted: ");
int index = sc.nextInt();
Scanner sc2 = new Scanner(System.in);
System.out.print("Enter the position: ");
int index1 = sc2.nextInt();
System.out.println(charRemoveAt(str, index, index1));
Rajeev Gandhi Memorial College of Engineering and Technology (Autonomous) 2019-2023-MECH-FDH
}
public static String charRemoveAt(String str, int n, int p) {
return str.substring(0, p) + str.substring(p + n + 1);
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter the String: coding
Enter the no. of characters to be deleted: 1
Enter the position: 1
cing