forked from skipboredom/hashcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindromeRemove.cpp
76 lines (42 loc) · 1.21 KB
/
palindromeRemove.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Remove palindromes words from the given sentence
#include <bits/stdc++.h>
using namespace std;
/* Palindrome check */
bool checkPalindrome(string str){
int i, j;
i = 0;
j = str.size()-1;
while(i<j){
if(str[i++] != str[j--]) // check from front and back
return false; // not a palindrome
}
return true;
}
/* Remove the Palindromes from the string now */
string removingPalindromesFromSentence(string str){
string outputStr = "", words = "";
//formation of the new string
str = str + " ";
int strSize = str.size();
// string traversal
for(int i = 0; i < strSize; i++){
// check if the string is not empty
if(str[i] != ' '){
words = words + str[i]; // assigning the words to the sentence
}
else{
//palindrome check then add it to the sentence
if(!(checkPalindrome(words)))
outputStr += words + " "; // adding the output words
// reassigning
words = "";
}
}
return outputStr;
}
int main(){
string str ="I am good" ; //declaring string
//getline(cin,str); //take input of the sentence
cout<<"Output String: "<<removingPalindromesFromSentence(str);
return 0;
}