Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <Windows.h>
  4. #include <iostream>
  5. #include <ctime>
  6. #define SP system("pause");
  7. #define CLS system("cls");
  8. using namespace std;
  9.  
  10. /*char* getback(const char* word1, char* word2) {
  11. char* save = word2;
  12. while (*word2 >= '\0') *word2++ = *word1++;
  13. return save;
  14. }*/
  15.  
  16. char* strdel1(char* word, int p, int k) {
  17. int word_str = strlen(word);
  18. if (p >= word_str || p + k >= word_str) {
  19. return 0;
  20. }
  21. for (int i = p + k; i <= word_str; ++i) {
  22. word[i - k] = word[i];
  23. }
  24. return word;
  25. }
  26.  
  27. char* strdel2(char* word, int p, int k) {
  28. int word_str = strlen(word), i = p + k;
  29. if (p >= word_str || i >= word_str) {
  30. return 0;
  31. }
  32. while (i <= word_str) {
  33. word[i - k] = word[i];
  34. i++;
  35. }
  36. return word;
  37. }
  38.  
  39. char* strdel3(char* word, int p, int k) {
  40. int word_str = strlen(word), i = p + k;
  41. if (p >= word_str || i >= word_str) {
  42. return 0;
  43. }
  44. do {
  45. word[i - k] = word[i];
  46. i++;
  47. } while (i <= word_str);
  48. return word;
  49. }
  50.  
  51. char* strdel4(char* word, int p, int k) {
  52. char* start = word + p, *end = word + p + k; //берем отрезок от p до k
  53. int word_str = strlen(word), i = p + k;
  54. if (p >= word_str || i >= word_str) {
  55. return 0;
  56. }
  57. while (*end != '\0') {
  58. *start++ = *end++;
  59. }
  60. *start = 0;
  61. return word;
  62. }
  63.  
  64. void testchamber() {
  65. srand(time(NULL));
  66. const char* sdel[] = { "Alexander","Germany","Dragon_Maid","9419942","12343","s@11ffs","Enigm@dd","kElH sI5","Ma7","aaab" };
  67. char stemp[256] = { "" };
  68. int p = rand() % 5;
  69. int k = rand() % 6 + 1;
  70. int test = sizeof(sdel) / sizeof(char*);
  71. printf("\nFunciton \"STRDEL\" search.test:");
  72. for (int i = 0; i < test; ++i) {
  73. printf("\nTest %i:\n", i + 1);
  74. strcpy(stemp, sdel[i]);
  75. printf("strdel1 { %s =del=> %s <-- p = %i, k = %i }\n", sdel[i], strdel1(stemp, p, k), p, k);
  76. strcpy(stemp, sdel[i]);
  77. printf("strdel2 { %s =del=> %s <-- p = %i, k = %i }\n", sdel[i], strdel2(stemp, p, k), p, k);
  78. strcpy(stemp, sdel[i]);
  79. printf("strdel3 { %s =del=> %s <-- p = %i, k = %i }\n", sdel[i], strdel3(stemp, p, k), p, k);
  80. strcpy(stemp, sdel[i]);
  81. printf("strdel4 { %s =del=> %s <-- p = %i, k = %i }\n", sdel[i], strdel4(stemp, p, k), p, k);
  82. }
  83. }
  84.  
  85. int main() {
  86. const char word1[] = { "Domashniy" };
  87. char word2[256] = { "" };
  88. int p = 2;
  89. int k = 4;
  90. strcpy(word2, word1);
  91. printf("Strdel1: { %s }\n", strdel1(word2, p, k));
  92. strcpy(word2, word1);
  93. printf("Strdel2: { %s }\n", strdel2(word2, p, k));
  94. strcpy(word2, word1);
  95. printf("Strdel3: { %s }\n", strdel3(word2, p, k));
  96. strcpy(word2, word1);
  97. printf("Strdel4: { %s }\n", strdel4(word2, p, k));
  98. testchamber();
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement