Question

In: Computer Science

Given a string, write a method called removeRepthat returns another string where adjacent characters that are...

  1. Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example:

removeRep(“yyzzza”) à “yza”

removeRep(“aabbbccd”) à “abcd”

removeRep(“122333”) à “123”

Solutions

Expert Solution

Here is the complete code filled with comments to help understand all the lines.

The code is in c++ but can also be applied to java and other languages as the logic remains the same.

//These are the required header file
#include <iostream>
#include <string>
using namespace std;

string removeRep(string str)
{
    //first we take the length of the string
    int len = str.size();
    //if length is less 2 we can simple return the string as it is
    if (len <= 1)
        return str;
    int j = 0;
    //we have kept the variable j to increase ONLY when we find a new unique element, thus there are no duplicates
    for (int i = 1; i < len; i++)
    {
        if (str[i] != str[j])
        {
            str[++j] = str[i];
        }
    }
    j++;
    //at last resize the string to the required length ie j
    str.resize(j);
    return str;
}

int main()
{
    cout << removeRep("aazzzpp") << "\n";
    cout << removeRep("aabbbccccddd") << "\n";
    cout << removeRep("abcd") << "\n";
    cout << removeRep("bppppaz") << "\n";
    return 0;
}

Here is the output for the above code


Related Solutions

1. Write a method called isPalindrome that accepts a string as a parameter and returns true...
1. Write a method called isPalindrome that accepts a string as a parameter and returns true if the string is a palindrome otherwise returns false. This method uses a stack and a Queue to test whether the given string parameter is a palindrome [ that is, whether the characters read the same both forward and backward. For example “race car”, and “Are we not drawn onward, to new era.” are Palindromes] They are palindrome sentences, not just a word. 2....
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns...
Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters' 'This line has extra space characters’ • Function name: reduceWhitespace • Number of parameters: one string line • Return value: one string line The main file should handle the file operations to read from a .txt file you create and call the function from the...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
/**    * Returns the string formed by alternating the case of the characters in   ...
/**    * Returns the string formed by alternating the case of the characters in    * the specified string. The first character in the returned string is in    * lowercase, the second character is in uppercase, the third character is    * in lowercase, the fourth character is in uppercase, and so on.    * Examples:    *    * <ul>    * <li><code>alternatingCaps("a")</code> returns <code>"a"</code>    * <li><code>alternatingCaps("ab")</code> returns <code>"aB"</code>    * <li><code>alternatingCaps("abc")</code> returns <code>"aBc"</code>    *...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The method signature is as follows:  public int count(String s)
write a python code that Returns a string composed of characters drawn, in strict alternation, from...
write a python code that Returns a string composed of characters drawn, in strict alternation, from s1 and s2. If one string is longer than the other, the excess characters are added to the end of the string as shown in the examples below #Example 1 - blend("ape", "BANANA") returns "aBpAeNANA" #Example 2 - blend("BOOT", "gold") returns "BgOoOlTd"
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT