Code Coach - It's a sign | Sololearn: Learn to code for FREE!

0

Code Coach - It's a sign

Input Format Four strings that represent the word or phrase that was supposed to be printed on the signs inside each box. Output Format A string that say 'Open' if at least one of the boxes is a palindrome or 'Trash' if all of the signs are misprinted. So here is my solution for this problem, idk why test case 3 says wrong, so help me maybe ✌🏻 #include <iostream> #include <string> using namespace std; int main() { string arr[4]; string word[] = {"aman", "abba", "bcba","car"}; int c = 0; for (int i = 0; i < 4; i++){ cin>>arr[i]; } for (int i = 0; i < 4; i++){ int x = arr[i].length(); for (int j = 0; j < x; j++){ word[i][j] = arr[i][x - j - 1]; } if (word[i] == arr[i]){ c++; } } //cout<<word[0]; if (c >= 1){ cout<<"Open"; } else cout<<"Trash"; return 0; }

8/14/2022 11:10:42 AM

Aman Kumar Singh

3 Answers

New Answer

+2

Aman Singh In short #include <iostream> #include <string> using namespace std; bool ispalim(string s) { string word; for (int j = 0; j < s.length(); j++) word += s[s.length() - j - 1]; return word == s; } int main() { string arr[4]; int c = 0; for (int i = 0; i < 4; i++) { cin >> arr[i]; if (ispalim(arr[i])) c++; } cout << (c >= 1 ? "Open" : "Trash"); return 0; }

+2

Aman Singh You are not reinitialising word array. #include <iostream> #include <string> using namespace std; bool ispalim(string s) { string word; for (int j = 0; j < s.length(); j++){ word += s[s.length() - j - 1]; } return word == s; } int main() { string arr[4]; string word[] = {}; int c = 0; for (int i = 0; i < 4; i++){ cin>>arr[i]; } for (int i = 0; i < 4; i++){ int x = arr[i].length(); // word[] = {}; /* for (int j = 0; j < x; j++){ word[i][j] = arr[i][x - j - 1]; }*/ if (ispalim(arr[i])) { c++; } } //cout<<word[0]; if (c >= 1){ cout<<"Open"; } else cout<<"Trash"; return 0; }

+1

A͢J Thanks It helped me ✌🏻