22.2k views
0 votes
You need a replace( ) method that will take in two parameters, the character to be replaced and the sequence of characters to replace it with. This method will use StringBuilder methods and logic to change all occurrences of the original string to a new string, while still preserving a copy of the original string.

1 Answer

0 votes

Answer:

mport java.util.Scanner;

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Surya

*/

public class string {

String s;

string(String n)

{

this.s = n;

}

// Print the number of characters your sentence contains

void numberofchars(String n)

{

System.out.println("The number of characters are :"+n.length());

}

//Print the first letter of your sentence

void print_firstchar(String n)

{

System.out.println("First char of the sentence :"+n.charAt(0));

}

//Print the last letter of your sentence

void print_lastchar(String n)

{

System.out.println("Last char of the sentence :"+n.charAt(n.length()-1));

}

//Print whether your sentence contains the letter ‘e’

void contain_e(String n)

it doesn't contain e");

//Print whether your sentence contains “ay”

void contain_ay(String n)

int i,m =n.length();

for(i=0;i<m-1;i++)

n.charAt(i)=='A')

if(n.charAt(i+1)=='y'

System.out.println("No

//Print the number of times the letter ‘e’ appears in your sentence

void numberof_e(String n)

{

int i,m =n.length();

int s=0;

for(i=0;i<m;i++)

{

if(n.charAt(i)=='e' || n.charAt(i)=='E')

{

s++;

}

}

System.out.println("e appears :"+s+ " times");

}

//ind the position of the last occurrence of the letter ‘e’ in your sentence

void position_of_lastoccurenceof_e(String n)

{

int i,m =n.length(),k=0;

for(i=0;i<m;i++)

{

if(n.charAt(i)=='e' || n.charAt(i)=='E')

{

k=i;

}

}

System.out.println("Position of last occurence of e "+k);

}

//Find the position of the second occurrence of the letter ‘e in your sentence

void position_of_second_occurenceof_e(String n)

{

int i,m =n.length(),k=0,s=0;

for(i=0;i<m;i++)

{

if(n.charAt(i)=='e' || n.charAt(i)=='E')

{

k=i;

s++;

if(s==2)break;

}

}

System.out.println("Position of second occurence of e "+k);

}

//number of characters your sentence contains besides the space character

void number_of_characters_beside_space(String n)

{

int i,m = n.length();

int sum=0;

for(i=0;i<m;i++)

{

if(n.charAt(i)!=' ')

sum++;

}

System.out.println("Number of characters beside space : "+sum);

}

public static void main(String argv[])

{

String str;

Scanner sc = new Scanner(System.in);

str =sc.nextLine();//reading line...

string s = new string(str);//creating object...intializing with str

//calling functions

s.numberofchars(str);

s.print_firstchar(str);

s.print_lastchar(str);

s.contain_e(str);

s.numberof_e(str);

s.contain_ay(str);

s.position_of_lastoccurenceof_e(str);

s.position_of_second_occurenceof_e(str);

s.number_of_characters_beside_space(str);

}

}

Step-by-step explanation:

User Nelo Angelo
by
7.8k points