Advertisement
Guest User

Light

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include"stdafx.h"
  2. #include<iostream>
  3. #include<stdio.h>
  4. #include <string.h>
  5.  
  6. const int M = 100, L = 255;
  7.  
  8. bool sym(char s[])
  9. {
  10.     int i, n = strlen(s) / 2;
  11.     if (strlen(s) % 2 == 0)
  12.     {
  13.         for (i = 0; i < n; i++)
  14.             if (s[n - i - 1] != s[n + i]) return false;
  15.     }
  16.     else
  17.     {
  18.         for (i = 1; i <= n; i++)
  19.             if (s[n - i] != s[n + i]) return false;
  20.     }
  21.     return true;
  22. }
  23.  
  24. void swap(char a[],char b[])
  25. {
  26.     char c[L];
  27.     strcpy(c, a);
  28.     strcpy(a, b);
  29.     strcpy(b, c);
  30. }
  31. void main()
  32. {
  33.     setlocale(LC_ALL, "Russian");
  34.  
  35.     int i, j, k, n, l, t, pause;
  36.     char s[M][L], a[L];
  37.  
  38.     for (i = 0; i<M; i++)
  39.     {
  40.         gets_s(s[i]);
  41.         if (!*s[i]) break;
  42.     }
  43.     n = i;
  44.  
  45.     strcpy(a, s[0]);
  46.     for (i = 1; i < n; i++)
  47.         strcat(a, s[i]);
  48.  
  49.     if (sym(a))
  50.         printf("Palindrome:\n%s\n", a);
  51.  
  52.     for (i = 0; i < n - 1; i++)
  53.     {
  54.         if (sym(a)) break;
  55.         for (j = i + 1; j <= n - 1; j++)
  56.         {
  57.             swap(s[i], s[j]);
  58.  
  59.             strcpy(a, s[0]);
  60.             for (k = 1; k < n; k++)
  61.                 strcat(a, s[k]);
  62.             printf("%s\n", a);
  63.  
  64.             if (sym(a))
  65.             {
  66.                 printf("Palindrome:\n%s\n", a);
  67.                 break;
  68.             }
  69.             swap(s[i], s[j]);
  70.         }
  71.     }
  72.  
  73.     scanf_s("%i", &pause);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement