fork(15) download
  1. // A C++ program to remove "a" and 'bc' from input string
  2. #include <iostream>
  3. using namespace std;
  4. #define ONE 1
  5. #define TWO 2
  6.  
  7. // A C++ program to remove "a" and 'bc' from input string
  8. #include <iostream>
  9. using namespace std;
  10. #define ONE 1
  11. #define TWO 2
  12.  
  13. // The main function that removes occurences of "a" and "bc" in input string
  14. void stringFilter(char *str)
  15. {
  16. // state is initially ONE (The previous character is not a)
  17. int state = ONE;
  18.  
  19. // i and j are index variables, i is used to read next character of input
  20. // string, j is used for indexes of output string (modified input string)
  21. int j = 0;
  22.  
  23. // Process all characters of input string one by one
  24. for (int i = 0; str[i] != '\0'; i++)
  25. {
  26. /* If state is ONE, then do NOT copy the current character to output if
  27.   one of the following conditions is true
  28.   ...a) Current character is 'b' (We need to remove 'b')
  29.   ...b) Current character is 'a' (Next character may be 'c') */
  30. if (state == ONE && str[i] != 'a' && str[i] != 'b')
  31. {
  32. str[j] = str[i];
  33. j++;
  34. }
  35.  
  36. // If state is TWO and current character is not 'c' (otherwise
  37. // we ignore both previous and current characters)
  38. if (state == TWO && str[i] != 'c')
  39. {
  40. // First copy the previous 'a'
  41. str[j] = 'a';
  42. j++;
  43.  
  44. // Then copy the current character if it is not 'a' and 'b'
  45. if (str[i] != 'a' && str[i] != 'b')
  46. {
  47. str[j] = str[i];
  48. j++;
  49. }
  50. }
  51.  
  52. // Change state according to current character
  53. state = (str[i] == 'a')? TWO: ONE;
  54.  
  55. if (j>1 && str[j-2]=='a' && str[j-1] =='c')
  56. j = j-2;
  57. }
  58.  
  59. // If last character was 'a', copy it to output
  60. if (state == TWO)
  61. {
  62. str[j] = 'a';
  63. j++;
  64. }
  65.  
  66. // Set the string terminator
  67. str[j] = '\0';
  68. }
  69.  
  70. /* Driver program to check above functions */
  71. int main()
  72. {
  73. char str1[] = "ad";
  74. stringFilter(str1);
  75. cout << str1 << endl;
  76.  
  77. char str2[] = "acbac";
  78. stringFilter(str2);
  79. cout << str2 << endl;
  80.  
  81. char str3[] = "aaac";
  82. stringFilter(str3);
  83. cout << str3 << endl;
  84.  
  85. char str4[] = "react";
  86. stringFilter(str4);
  87. cout << str4 << endl;
  88.  
  89. char str5[] = "aa";
  90. stringFilter(str5);
  91. cout << str5 << endl;
  92.  
  93. char str6[] = "ababaac";
  94. stringFilter(str6);
  95. cout << str6 << endl;
  96.  
  97. char str7[] = "abc";
  98. stringFilter(str7);
  99. cout << str7 << endl;
  100.  
  101. char str8[] = "aaacabccabcaxc";
  102. stringFilter(str8);
  103. cout << str8 << endl;
  104.  
  105. char str9[] = "abcaaccd";
  106. stringFilter(str9);
  107. cout << str9 << endl;
  108.  
  109. char str10[] = "aaxaacccxcac";
  110. stringFilter(str10);
  111. cout << str10 << endl;
  112.  
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
ad

aa
ret
aa
aaa

aaxc
d
aaxcxc