puma7953

Untitled

Nov 26th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int my_strlen(char *str) {
  7.     int n = 0;
  8.     while (str[n] != '\0')
  9.         n++;
  10.     return n;
  11. }
  12.  
  13. void my_strcpy(char *source, char *target) {
  14.     int i = 0;
  15.     while ((source[i] = target[i]) != '\0')
  16.         i++;
  17.     source[i] = target[i];
  18. }
  19.  
  20. int my_strstr(char *haystack, char *needle) {
  21.     int k, count = 0, len, c = 0;
  22.     len = my_strlen(needle);
  23.     for (int i = 0; haystack[i] != '\0'; i++) {
  24.         c++;
  25.         k = i;
  26.         for (int j = 0; needle[j] != '\0'; j++) {
  27.             if (haystack[k] != needle[j])
  28.                 break;
  29.             count++;
  30.             k++;
  31.         }
  32.         if (count == len)
  33.             return i + 1;
  34.         count = 0;
  35.     }
  36.     return -1;
  37. }
  38.  
  39. int main() {
  40.     char a[] = "";                          
  41.     int method;
  42.     cout << "Enter the desired function (strlen - 1, strcpy - 2, strstr - 3)" << endl;
  43.     cin >> method;
  44.     if (method == 1) {
  45.         int kol = my_strlen(a);
  46.         cout << kol;
  47.     }
  48.  
  49.     if (method == 2) {
  50.         char b[] = "";
  51.         my_strcpy(a, b);
  52.         for (int i = 0; i < my_strlen(a); i++){
  53.             cout << a[i];
  54.         }
  55.     }
  56.     if (method == 3) {
  57.         char b[] = "";
  58.         cout << my_strstr(a, b);
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment