Insert A New Character | Delete A Character | Append Two Strings | Reverse A String


Output


write a java program to perform following operations insert a new character delete a character append two strings reverse a string


Code

import java.util.Scanner;

abstract class abs{

StringBuffer str;

Scanner scan=new Scanner(System.in);

abstract void ins();

abstract void del();

abstract void app();

abstract void rev();

}

class operations extends abs{

operations(StringBuffer str){

this.str=str;

}

void ins(){

dis(str);

System.out.print("Enter Inserting String : ");

String s1=scan.next();

s1=" "+s1+" ";

int i=str.length()+1;

while(i>str.length()){

System.out.print("Enter Inserting Index : ");

i=scan.nextInt();

}

System.out.print("After Inserting : ");

dis(str.insert(i,s1));

}

void del(){

dis(str);

int s1=str.length();

while(s1>=str.length()){

System.out.print("Enter Delete Starting Index : ");

s1=scan.nextInt();

}

int i=str.length()+1;

while(i>str.length() && s1<i){

System.out.print("Enter Delete Last Index : ");

i=scan.nextInt();

}

System.out.print("After Delete : ");

dis(str.delete(s1,i));

}

void app(){

dis(str);

System.out.print("Enter Apped String : ");

String st=scan.next();

System.out.print("After Appending : ");

dis(str.append(" ").append(st));

}

void rev(){

dis(str);

System.out.print("Reverse String :");

dis(str.reverse());

}

void dis(StringBuffer str){

System.out.println("This is String : "+str);

this.str=str;

}

}

class String_5{

public static void main(String args[]){

operations op;

Scanner scan=new Scanner(System.in);

System.out.print("Enter String : ");

String s1=scan.next();

StringBuffer sb=new StringBuffer(s1);

op=new operations(sb);

int c=1;

while(c!=5){

System.out.print("operations :\n1.Insert a new character\n2.Delete a character\n3.Append two strings\n4.Reverse a string\n5.Exit\nEnter selection : ");

c=scan.nextInt();

switch(c){

case 1: op.ins(); break;

case 2: op.del(); break;

case 3: op.app(); break;

case 4: op.rev(); break;

case 5: System.out.print("Exit...."); break;

default : System.out.print("Invalid....");

}

}

}

String Handling In Java

TCP Protocol 

Comment your views on this Article :)


Thank you for visiting my blog :)

No comments

Comment your views on this article

Powered by Blogger.