Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(10 pt) Implement a function c creates a new string (ns) that will contain the u

ID: 3891795 • Letter: #

Question


(10 pt) Implement a function c creates a new string (ns) that will contain the unique characters from the given string str. For example: char ns; 1. har unique chars (char "str) which dynamically ns -unique_chars ("aaaabbbbAAAAAACcccaaaAAAbbb") ns should be pointing to a dynamically created new string "abAc" nsunique_chars ("abcABCD") ns should be pointing to a dynamically created new string "abcABCD" If needed, yoy can use standard library functions like strlen, strcpy (dest, src), strchr (char .s, char ch), which returns NULL if ch is not in s. Allocating large spaces (eg, as large as str) might be needed temporarily. But, at the end, make su you allocate just enough space for the new string ns and free temporarily allocated spaces (if any char * unique chars (char *str) char *ns, tmp int len, i,j

Explanation / Answer

Please find the required code:

//===================================================================

#include <iostream>
#include<string.h>

#define MAX_LEN 100

using namespace std;

char* unique_chars(char*str)
{
    char*val=new char[MAX_LEN];
    int l=strlen(str),c=0;
    for(int i=0;i<l;i++)
    {   // We start by takng an empty strin val and check for the presence of a
        // character in it. If the character is not present in val, update it, else do nothing
        if(!strchr(val,str[i]))
        {
            val[c]=str[i];
            c++;
        }
    }
return (val);
delete(val);
}


int main() {
char *str=new char[MAX_LEN];
char *temp=new char[MAX_LEN];
fgets(temp, MAX_LEN, stdin);
str=unique_chars(temp);
std::cout <<"The string of unique characters is: "<< str << std::endl;
delete(str);
delete(temp);
return 0;
}

//===================================================================

Sample output:

Please provide string input: aaaAbbbBCCCcCdffD

The string of unique characters is: aAbBCcdfD

Hope this helps!