(10 pt) Implement a function c creates a new string (ns) that will contain the u
ID: 3891795 • Letter: #
Question
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!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.