Advertisement
emaansahmed

Assignment #5

Apr 26th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. // Recursively removes adjacent duplicates from str and returns new
  6. // string. las_removed is a pointer to last_removed character
  7. char* removeUtil(char *str, char *last_removed)
  8. {
  9.     // If length of string is 1 or 0
  10.     if (str[0] == '\0' || str[1] == '\0')
  11.         return str;
  12.  
  13.     // Remove leftmost same characters and recur for remaining string
  14.     if (str[0] == str[1])
  15.     {
  16.         *last_removed = str[0];
  17.         while (str[1] && str[0] == str[1])
  18.             str++;
  19.         str++;
  20.         return removeUtil(str, last_removed);
  21.     }
  22.  
  23.     // At this point, the first character is definiotely different from its
  24.     // adjacent. Ignore first character and recursively remove characters from
  25.     // remaining string
  26.     char* rem_str = removeUtil(str+1, last_removed);
  27.  
  28.     // Check if the first character of the rem_string matches with the
  29.     // first character of the original string
  30.     if (rem_str[0] && rem_str[0] == str[0])
  31.     {
  32.         *last_removed = str[0];
  33.         return (rem_str+1); // Remove first character
  34.     }
  35.  
  36.     // If remaining string becomes empty and last removed character
  37.     // is same as first character of original string.  This is needed
  38.     // for a string like "acbbcddc"
  39.     if (rem_str[0] == '\0' && *last_removed == str[0])
  40.          return rem_str;
  41.  
  42.     // If the two first characters of str and rem_str don't match, append
  43.     // first character of str before the first character of rem_str.
  44.     rem_str--;
  45.     rem_str[0] = str[0];
  46.     return rem_str;
  47. }
  48.  
  49. char *remove(char *str)
  50. {
  51.     char last_removed = '\0';
  52.     return removeUtil(str, &last_removed);
  53. }
  54.  
  55. // Driver program to test above functions
  56. int main()
  57. {
  58.     char str1[] = "geeksforgeeg";
  59.     cout << remove(str1) << endl;
  60.  
  61.     char str2[] = "azxxxzy";
  62.     cout << remove(str2) << endl;
  63.  
  64.     char str3[] = "caaabbbaac";
  65.     cout << remove(str3) << endl;
  66.  
  67.     char str4[] = "gghhg";
  68.     cout << remove(str4) << endl;
  69.  
  70.     char str5[] = "aaaacddddcappp";
  71.     cout << remove(str5) << endl;
  72.  
  73.     char str6[] = "aaaaaaaaaa";
  74.     cout << remove(str6) << endl;
  75.  
  76.     char str7[] = "qpaaaaadaaaaadprq";
  77.     cout << remove(str7) << endl;
  78.  
  79.     char str8[] = "acaaabbbacdddd";
  80.     cout << remove(str8) << endl;
  81.  
  82.     char str9[] = "acbbcddc";
  83.     cout << remove(str9) << endl;
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement