Write a function norepetitions which removes all repetitions

Write a function \"no_repetitions(...)\" which removes all repetitions of characters from a string. Test the function in a suitable main program, which should be able to reproduce the following input/output:

Try to solve the program by using the string datatype rather than a char array char a[ ].

Solution

#include<bits/stdc++.h>
using namespace std;

char *removeDuplicates(char *str)
{
   int resultIndex = 1, currentIndex = 1;

   //1. In place removal of duplicate characters
   while (*(str + currentIndex))
   {
       if (*(str + currentIndex) != *(str + currentIndex - 1))
       {
           *(str + resultIndex) = *(str + currentIndex);
           resultIndex++;
       }
       currentIndex++;
   }

   //2. Remove extra characters by putting string end character after resultIndex
   *(str + resultIndex) = \'\\0\';

   return str;
}

char *no_repetitions(char *str)
{
//1. Sort string
sort(str, str+strlen(str));
  
//2. Remove duplicates from sorted
return removeDuplicates(str);
}

int main()
{
string str;
cout << \"Enter string: \";
cin >> str;
getline(cin, str);
cout << no_repetitions(&str[0u]); //converted string to char*
return 0;
}

Write a function \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site