In: Computer Science
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).
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// ReverseReplaceRemoveCharDemo.java
import java.util.Scanner;
public class ReverseReplaceRemoveCharDemo {
public static void main(String[] args) {
String
str,revStr="",afterReplaced="",charRemoved="";
/*
* Creating an Scanner class object
which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
//Getting the input entered by
the user
System.out.print("Enter String :");
str=sc.nextLine();
for(int i=str.length()-1;i>=0;i--)
{
revStr+=str.charAt(i);
}
System.out.println("After reverse '"+str+"' is :"+revStr);
System.out.print("Enter string you want to replace :");
String replaceStr=sc.nextLine();
System.out.print("Enter string you want to replace with:");
String replaceWith=sc.nextLine();
int indx=str.indexOf(replaceStr);
if(indx!=-1)
{
afterReplaced=str.substring(0,indx);
afterReplaced+=replaceWith;
afterReplaced+=str.substring(indx+1+replaceStr.length()-1,str.length());
System.out.println("After replacing '"+replaceWith+"'
, the new String is :"+afterReplaced);
}
else
{
System.out.println("'"+replaceStr+"' is not substring
of '"+str+"'");
}
System.out.print("Enter char to remove from '"+str+"':");
char ch=sc.next(".").charAt(0);
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)!=ch)
{
charRemoved+=str.charAt(i);
}
}
System.out.println("String after removal of '"+ch+"'
:"+charRemoved);
}
}
___________________________
Enter String :you are so beautiful.
After reverse 'you are so beautiful.' is :.lufituaeb os era
uoy
Enter string you want to replace :beautiful
Enter string you want to replace with:ugly
After replacing 'ugly' , the new String is :you are so ugly.
Enter char to remove from 'you are so beautiful.':a
String after removal of 'a' :you re so beutiful.
_______________Could you plz rate me well.Thank You