Question

In: Computer Science

public static java.lang.String removerec(java.lang.String s) Returns a string similar to the given string with all runs...

public static java.lang.String removerec(java.lang.String s)

Returns a string similar to the given string with all runs of consecutive, repeated characters removed. For example,

  • given "apple", returns "aple"
  • given "banana", returns "banana"
  • given "baaannannnnaa", returns "banana"
  • given an empty string, returns an empty string

Parameters:

s - given string

Returns:

string created from s by removing runs of the same character

Solutions

Expert Solution

Code:

public class Main
{
    public static String removerec(String s)
    {
        /*Create empty string*/
        String res="";
        int n=s.length();
        for(int i=1;i<n;i++)
        {
            /*If a chacater not equals to it's previous character*/
            if(s.charAt(i-1)!=s.charAt(i))
            {
                /*append previous character to cretaed string*/
                res=res+s.charAt(i-1);
            }
        }
        /*append last character of string*/
        res=res+s.charAt(n-1);
        return res;
    }
        public static void main(String[] args) 
        {
                System.out.println(removerec("apple"));
                System.out.println(removerec("banana"));
                System.out.println(removerec("baaannannnnaa"));
        }
}

Screenshots:


Related Solutions

QUESTION 11 In util package, public class Example { public static void showMessage( String s ){...
QUESTION 11 In util package, public class Example { public static void showMessage( String s ){ ....... } public static int getInt( String prompt ){ ....... } } Using these method you see above to create a main method in your class called MyExam in the different package to get input and output. The output will tell the user what number is entered. If you entered a 5, the output will be: "The number you entered is 5.". QUESTION 12...
Given the following TestDriver.java public class TestDriver {       public static void main(String args[]) {             Point point...
Given the following TestDriver.java public class TestDriver {       public static void main(String args[]) {             Point point = new Point(10, 5);       Circle circle = new Circle(10.5, 20, 19);       Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);                     System.out.println("Point");       System.out.println(point.toString()) ;       System.out.println("Circle");       System.out.println(circle.toString());       System.out.println("Cylinder");       System.out.println(cylinder.toString());       } } You will get the following result once you execute the TestDriver.java (after compiling it) Point (10, 5) Circle Center = (20, 19); Radius = 10.5 Cylinder Center = (20, 20); Radius = 3.6; Height...
Given the following TestDriver.java public class TestDriver {       public static void main(String args[]) {             Point point...
Given the following TestDriver.java public class TestDriver {       public static void main(String args[]) {             Point point = new Point(10, 5);       Circle circle = new Circle(10.5, 20, 19);       Cylinder cylinder = new Cylinder(12, 3.6, 20, 20);                     System.out.println("Point");       System.out.println(point.toString()) ;       System.out.println("Circle");       System.out.println(circle.toString());       System.out.println("Cylinder");       System.out.println(cylinder.toString());       } } You will get the following result once you execute the TestDriver.java (after compiling it) Point (10, 5) Circle Center = (20, 19); Radius = 10.5 Cylinder Center = (20, 20); Radius = 3.6; Height...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT