Ask Question
30 November, 06:14

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.

+4
Answers (1)
  1. 30 November, 06:33
    0
    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)

    int i, m = n. length ();

    for (i=0; i
    n. charAt (i) = ='E')

    it contain e");

    return;

    System. out. println ("No

    //Print whether your sentence contains "ay"

    void contain_ay (String n)

    it doesn't contain ay");

    //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
    {

    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
    {

    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
    {

    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
    {

    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);

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “You need a replace () method that will take in two parameters, the character to be replaced and the sequence of characters to replace it ...” in 📗 Engineering if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers