Advertisement
rado_dimitrov66

Char String

Dec 5th, 2023 (edited)
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void convertAllCharacter(char str[])
  6. {
  7.     for (int i = 0; i < strlen(str); i++)
  8.     {
  9.         if (str[i] >= 'A' && str[i] <= 'Z')
  10.         {
  11.             str[i] += 32;
  12.         }
  13.         else if (str[i] >= 'a' || str[i] <= 'z')
  14.         {
  15.             str[i] -= 32;
  16.         }
  17.  
  18.  
  19.     }
  20.  
  21.     cout << str << endl;
  22. }
  23.  
  24.  
  25. void convertAllToUpperOrLower(char str[])
  26. {
  27.     unsigned short choose = 0;
  28.     do
  29.     {
  30.         cout << "1 -[lower to upper]\n2 - [upper to lower]\n";
  31.         cin >> choose;
  32.  
  33.     } while (choose < 1 || choose >> 2);
  34.  
  35.     switch (choose)
  36.     {
  37.     case 1:
  38.  
  39.         for (int i = 0; i < strlen(str); i++)
  40.         {
  41.             if (str[i] >= 'a' && str[i] <= 'z')
  42.             {
  43.                 str[i] -= 32;
  44.             }
  45.         }
  46.  
  47.         break;
  48.  
  49.     case 2:
  50.  
  51.         for (int i = 0; i < strlen(str); i++)
  52.         {
  53.             if (str[i] >= 'A' && str[i] <= 'Z')
  54.             {
  55.                 str[i] += 32;
  56.             }
  57.         }
  58.  
  59.     default:
  60.         break;
  61.     }
  62.  
  63.     cout << str << endl;
  64. }
  65.  
  66. bool isPalindrom(char str[])
  67. {
  68.  
  69.     for (int i = strlen(str) - 1; i >= 0; i--) {
  70.  
  71.         if (str[i] != str[(strlen(str) - 1) - i]) {
  72.             return false;
  73.         }
  74.     }
  75.  
  76.  
  77.     return true;
  78.  
  79. }
  80.  
  81. int main()
  82. {
  83.     char str[20];
  84.     unsigned int choose;
  85.  
  86.     cout << "Enter value of char string: ";
  87.     cin >> str;
  88.  
  89.     do
  90.     {
  91.  
  92.         cout << "Enter value of function:\n[1] lower to upper and upper to lower:\n[2] lower to upper or upper to lower:\n[3] is Palindrom ?\n";
  93.         cin >> choose;
  94.  
  95.         switch (choose)
  96.         {
  97.         case 1:
  98.  
  99.             convertAllCharacter(str);
  100.  
  101.             choose = -1;
  102.             break;
  103.  
  104.         case 2:
  105.  
  106.             convertAllToUpperOrLower(str);
  107.  
  108.             choose = -1;
  109.             break;
  110.  
  111.         case 3:
  112.             if (isPalindrom(str)) {
  113.                 cout << "This char string is palindrom" << endl;
  114.             }
  115.             else {
  116.                 cout << "This char string is not palindrom" << endl;
  117.             }
  118.             choose = -1;
  119.             break;
  120.         default:
  121.             break;
  122.         }
  123.  
  124.  
  125.     } while (choose < 0 || choose > 6);
  126.  
  127. }
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement