How can you check if the elements in an array are palindromic using C++? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

How can you check if the elements in an array are palindromic using C++?

I have seen several methods that use cin input for the string but I have to use a predetermined array. ANy help or tips would be greatly appreciated.

20th Apr 2017, 12:32 PM
TaimuKo
6 ответов
+ 2
You can use two pointers. 1 at the start and 1 at the end and if pointers end and start equal each other increment the pointers in farther till they reach the middle. else the word is not palindromic.
20th Apr 2017, 8:52 PM
Ty Ler
Ty Ler - avatar
+ 1
You send the array and the length of it to the function and it will return true if the array is palindromic else false. You can also change the type of the array and it will work on any kind of array (in the function parameters)
20th Apr 2017, 12:47 PM
G4S
+ 1
Your welcome :)
20th Apr 2017, 12:57 PM
G4S
0
bool isPalindrom(int* arr, int length) { int i; int j; for(i=0, j=length-1; i!= j; i++, j--) { if(i + 1 == j && arr[i] == arr [j]) return true; if(arr[i] != arr[j]) return false; } return true; }
20th Apr 2017, 12:42 PM
G4S
0
Thank you and how would I use that function for the array. Sorry I am new to C++ coding.
20th Apr 2017, 12:43 PM
TaimuKo
0
Okay thank you for your assistance :)
20th Apr 2017, 12:51 PM
TaimuKo