Remove adjacent duplicate characters - in C++ Langauge

 Remove adjacent duplicate characters - in C++ Langauge 

Given a string, write a program to recursively remove adjacent duplicate characters from string. The output string should not have any adjacent duplicates. See following examples.

Input:  azxxzy
Output: ay
[Hint: First "azxxzy" is reduced to "azzy". The string "azzy" contains duplicates, so it is further reduced to "ay"].

Input: caaabbbaacdddd
Output: -1

Input: acaaabbbacdddd
Output: acac

Input Format
Input contains a single string . See the sample input section for details.

Business Rules
       Input string should not contain any special characters or whitespace. Display "Invalid Input" to the screen if voilates any of these constraints.
       If the output string doesn't contain any character after removing the adjacent duplicate characters from the string, then it should print -1.
          Maximum length of the string should be 50. Else display "Size of the string is too large".

Output Format
Print the ouput string after  removing the adjacent duplicate characters from the input string.

Sample Input 1:
caaabbbaacdddd
Sample Output 1
acac

Sample Input 2:
caaabbbaacdddd
Sample Output 2:
-1

Sample Input 3:
abcd@
Sample Output 3:
Invalid Input

Sample Input 4:
youaregivenastringswriteaprogramtofindthelengthofthestring
Sample Output 4:
Size of the string is too large

Image result for c++ coding


Note :
Function specification is the following for C#,Java and C++.
Name:- RemoveDuplicateChars
Return Type:- string
Parameter(s):- string input1

#include #include using namespace std; char* removeDup(char * str, int n) { int len = strlen(str); int k = 0; for (int i=1; i< len; i++) { if (str[i-1] != str[i]) str[k++] = str[i-1]; else while (str[i-1] == str[i]) i++; } str[k++] = str[i-1]; str[k] = '\0'; if (k != n) removeDup(str, k); else return str; } int main() { char str[] = "azxxxzy";

cout << removeDup(str2, strlen(str1)) << endl;
char str2[] = "caaabbbaac";
cout << removeDup(str3, strlen(str2)) << endl;
char str3[] = "aaaacddddcappp";
cout << removeDup(str5, strlen(str3)) << endl;
char str4[] = "aaaaaaaaaa";
cout << removeDup(str6, strlen(str4)) << endl;
char str5[] = "qpaaaaadaaaaadprq";
cout << removeDup(str7, strlen(str5)) << endl;
char str6[] = "acaaabbbacdddd";
cout << removeDup(str8, strlen(str6)) << endl;
char str7[] = "acbbcddc";
cout << removeDup(str9, strlen(str7)) << endl;
}

No comments