Question

This is in C++.

(10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
#include<string>

using namespace std;

string reduplicate(string input)
{
    string s;
    int len = input.length();
    if(len == 1)
    {
        string c(1, input.at(0));
        s.append(c);
    }
    else
    {
        for(int i = 0; i < len; i++)
         {
             s = "";
              for (int j = 0; j < len-1; j++)
                {
                        char ch = input.at(j);
                        if(ch != input.at(j+1)){
                           string c(1, input.at(j));
                           s.append(c);
                        }
                        else {
                          j++;
                        }

                        if(j == len-2) {
                            string c(1, input.at(j+1));
                           s.append(c);
                        }

                }
            input = s;
            len = input.length();

         }
    }


    return s;
}

int main()
{

 cout << reduplicate("AABB") << endl; // NULL
 cout << reduplicate("A") << endl; // A
 cout << reduplicate("ABBA") << endl;// NULL
 cout << reduplicate("AAA") << endl; // A
 cout << reduplicate("AKA") << endl; // AKA


 return 0;
}
Add a comment
Know the answer?
Add Answer to:
This is in C++. (10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I USE NETBEANS. C++ PLEASE. COULD YOU PLEASE WRITE A CODE THAT ACTUALLY WORKS, PLEASE PLEASE PLEA...

    I USE NETBEANS. C++ PLEASE. COULD YOU PLEASE WRITE A CODE THAT ACTUALLY WORKS, PLEASE PLEASE PLEASE EXPLAIN WHAT EVERYTHING IS DOING. INCLUDE SCREENSHOTS OF OUTPUTS PLEASE. I REALLY APPRECIATE THE HELP. THANK YOU <3 100% RATING IF CORRECT AND IS UNDERSTANDABLE. PLEASE ONLY CODES THAT WORK AND DO THE TASK AT HAND. 2 Write code for these functions using the string API: A boolean function no_el1 that takes a string as parameter and returns true if the string does...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • You are to write two functions, printString() and testString(), which are called from the main function....

    You are to write two functions, printString() and testString(), which are called from the main function. printString (string) prints characters to std::cout with a space after every character and a newline at the end. testString (string) returns true if the string contains two consecutive characters that are the same, false otherwise. See the main() to see how the two functions are called. Some sample runs are given below: string: “hello” printString prints: h e l l o testString returns: true...

  • Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions....

    Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions. int letter_count(char *s) computes and returns the number of English letters in string s. int word_count(char *s) computes and returns the number of words in string s. void lower_case(char *s) changes upper case to lower case of string s. void trim(char *s) removes the unnecessary empty spaces of string s. mystring.h #include <stdio.h> int letter_count(char *); void lower_case(char *); int word_count(char *); void trim(char...

  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive...

    Add a recursive Boolean function called checkRecurse to class IntegerLinkedList to check if any two consecutive integers in the linked list are equal (return true in this case, false otherwise). You may assume that the list is not empty. A recursion “helper” function is already included in class IntegerLinkedList. You only need to write the recursive function. For example, checkRecurseHelper() should return true for the linked list shown below. A main function (prob3.cpp) is given to you to add data...

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

  • C++ Write a recursive function that reverses the given input string. No loops allowed, only use...

    C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. #include <iostream> #include <string> using namespace std; //Write a recursive function 'void reverse(string &str)' that reverses the given input string. void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sherry";    reverse(name);    cout << name << endl; //should...

  • Complete a partially written C++ program that includes a function that return a value. The program...

    Complete a partially written C++ program that includes a function that return a value. The program is a simple calculator that prompts the user of 2 number and an operation (+, -, * or, /). The two number and the operator are passed to the function where the appropriate arithmetic operation is performed. The result is return to the main function () where the arithmetic operation and result are displayed. For example 3 * 4 = 12 The source code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT