Advertisement
Bertran_rz

03.11 home work

Nov 14th, 2021
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. char* copystr(char*);
  8.  
  9. char* phone_number(char*);
  10. void char_change(char*, char, char);
  11. bool pali_text(char*);
  12. void delete_char(char*&, char);
  13.  
  14. int main()
  15. {
  16.     cout << "First exercise\n";
  17.     //First exercise
  18.     char s[] = "+38(099)714-55-12";
  19.     cout << phone_number(s) << endl;
  20.    
  21.     cout << "Second exercise\n";
  22.     //Second exercise
  23.     char a[] = "fsdfsd fsdfsd fsdfewrf fsdfsdf";
  24.    
  25.     cout << a << endl;
  26.     cout << endl;
  27.  
  28.     char_change(a, ' ', 9);
  29.  
  30.     cout << a << endl;
  31.     cout << endl;
  32.  
  33.     cout << "Third exercise\n";
  34.     //third exercise
  35.     char b[] = "ababa";
  36.     cout << pali_text(b) << endl;
  37.  
  38.     cout << "Last exercise \n";
  39.     //last exercise
  40.  
  41.     char* z = copystr(a);
  42.     delete_char(z, 'f');
  43.  
  44.     cout << z << endl;
  45.  
  46. }
  47.  
  48. char* copystr(char* str){
  49.     char* tmp = nullptr;
  50.     tmp = new char[strlen(str)+1];
  51.     for(int i = 0; str[i] != '\0'; i++)
  52.         tmp[i] = str[i];
  53.  
  54.     tmp[strlen(str)] = '\0';
  55.     return tmp;
  56. }
  57.  
  58. //first exercise
  59. char* phone_number(char* string)
  60. {
  61.     char* tmp = new char[20];
  62.  
  63.     for(int i = 0, j = 0; string[i] != '\0'; i++)
  64.     {
  65.         if(isdigit(string[i]))
  66.         {
  67.             tmp[j] = string[i];
  68.             j++;
  69.         }
  70.     }
  71.     return copystr(tmp);
  72. }
  73.  
  74. //secon exercise
  75. void char_change(char* string, char in, char out)
  76. {
  77.     for(int i = 0; string[i] !='\0'; i++)
  78.         if(string[i] == in) string[i] = out;
  79. }
  80. //third exercise
  81. bool pali_text(char* string)
  82. {
  83.     for(int i = 0, j = strlen(string)-1; i <= strlen(string)/2;i++)
  84.         if(string[i] != string[j-i])
  85.             return false;
  86.     return true;
  87. }
  88. //last exercise
  89. void delete_char(char*& string, char chr)
  90. {
  91.     char* tmp = new char[100];
  92.  
  93.     for(int i = 0, j = 0; string[i] != '\0'; i++)
  94.     {
  95.         if(string[i] != chr)
  96.         {
  97.             tmp[j] = string[i];
  98.             j++;
  99.         }
  100.     }
  101.  
  102.     delete[] string;
  103.  
  104.     string = copystr(tmp);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement