Check the number in the array for the presence of palindrome (it is through an array).
Decision:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> #include <cstring> using namespace std; int main() { setlocale(LC_ALL, "rus"); char s[20]; while (true) { cout << "Введите любое положительное целое : "; cin >> s; bool poli = true; for (unsigned i = 0; i <= strlen(s) / 2 + 1; i++) if (!(poli = s[i] == s[strlen(s) - i - 1])) break; cout << "число " << (poli ? "" : "не ") << "палиндром" << endl; } return 0; } |
The only place, which code may cause difficulty, is a logical assignment:
1 | poli = s[i] == s[strlen(s) - i - 1] |
There is verified characters equality, otstoyashtih of i position from the beginning of the end of the study line. If the result is negative (not equal), then it is not a palindrome, and can leave the cycle, and further comparison is not carried out. Variable poli here it is not necessary, and left for clarity – you can make a decision (if) directly from the checked conditions.
Checks are made until the middle strings, because the further continuation of the cycle (due to the symmetry conditions) Only then will repeat, what has been done previously.
Performance:
As can easily be noted observational readers, This code will check on the palindrome is not only a record number, but any text string, when these conditions are met:
- row contains only Latin (not Russians, not Chinese, …) letters (international letters into Unicode seem to be more than the 1st byte);
- line does not contain blank spaces and punctuation marks (because the definitions of these symbols are palindromes, usually, excluded from the comparisons: “I ydu courts with sword“, “On the forehead, blockhead“).
Error code. All the two-digit numbers, for example: 11, 22, 33, …, 99 the program recognizes both “not palindromes”, although it is not so. The code needs to be improved.
no error! Here check runs in the operating system Linux with GCC compiler, which is much more representative of the standard C ++ syntax:
$ ./palindrom-
Введите любое положительное целое : 11
число палиндром
Введите любое положительное целое : 2
число палиндром
Введите любое положительное целое : 232
число палиндром
Введите любое положительное целое : 233
число не палиндром
Введите любое положительное целое : 2332
число палиндром
Введите любое положительное целое : 23432
число палиндром
Введите любое положительное целое : 234432
число палиндром
Введите любое положительное целое : ^C
I do not know, why you have obtained the following results (You may need to arrange additional brackets for priority operations).
And how Palindrome in the PHP array will be written ?
And as Palindrome in an array in PHP language will be
Trading company "Vostok" at the end of each month sums up the results, that is, it enters the book of expenses and sales proceeds. Will compose a program to calculate the amount of income received for each quarter and determine in which quarter the minimum income was received.