Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <conio.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #define clr system("cls")
- #define pause system("pause")
- using namespace std;
- int strlen1(char s[]) {
- int numb = 0;
- while (s[numb] != 0)
- numb++;
- return numb;
- }
- int strlen2(char s[]) {
- int numb = 0;
- for (int i = 0; i < 256; i++) {
- if (s[i] != 0)
- numb++;
- else break;
- }
- return numb;
- }
- int strlen3(char s[]) {
- int numb = 0, i = 0;
- begin:if (s[i] != 0) {
- numb++;
- i++;
- goto begin;
- }
- return numb;
- }
- int strlen4(char s[]) {
- int numb = 0, i = 0;
- char *s1 = s;
- begin:if (*s1 != 0) {
- numb++;
- *s1++;
- goto begin;
- }
- return numb;
- }
- char* strcopy1(char *dest, char *s, int p, int k) {
- int lens = strlen1(s);
- if (p < 1 || k < 1)
- return dest;
- if (p + k > lens)
- k = lens - p;
- for (int i = 0; i < k; i++, p++) {
- dest[i] = s[p];
- }
- return dest;
- }
- char* strcopy2(char *dest, char *s, int p, int k) {
- int lens = strlen1(s);
- if (p < 1 || k < 1 || p > strlen1(s))
- return dest;
- if (p + k > lens)
- k = lens - p;
- int i = 0;
- p++;
- while (k != 0 && dest != '\0') {
- *(dest + i) = *(s + p - 1);
- i++;
- p++;
- k--;
- }
- return dest;
- }
- char* strcopy3(char *dest, char *s, int p, int k) {
- int lens = strlen1(s);
- if (p < 1 || k < 1 || p > strlen1(s))
- return dest;
- if (p + k > lens)
- k = lens - p;
- for (int i = 0; s[i]; p++, i++) {
- if (s[p] == 0 || i == k)
- break;
- dest[i] = s[p];
- }
- return dest;
- }
- char* strcopy4(char *dest, char *s, int p, int k) {
- int lens = 0;
- lens = strlen1(s);
- if (p < 1 || k < 1 || p > lens) {
- return dest;
- }
- if (p + k > lens)
- k = lens - p;
- int i = 0;
- begin:if (k > 0) {
- dest[i] = s[p];
- p++;
- i++;
- k--;
- goto begin;
- }
- return dest;
- }
- void strins1(char *sub, char *s, int p) {
- int lens = strlen1(s);
- if (p > lens)
- p = lens;
- if (p < 1)
- p = 0;
- char tmp[256]{ 0 };
- for (int i = 0, k = p; k < strlen1(s); i++, k++)
- tmp[i] = s[k];
- for (int i = 0, k = p; k < strlen1(sub) + p; i++, k++)
- s[k] = sub[i];
- for (int i = 0, k = (strlen1(sub) + p); k < (strlen1(s) + strlen1(sub)); i++, k++)
- s[k] = tmp[i];
- }
- void strins2(char *sub, char *s, int p) {
- int lens = strlen1(s);
- if (p > lens)
- p = lens;
- if (p < 1)
- p = 0;
- for (int i = p, k = 0; i < strlen1(sub) + p; i++, k++) {
- for (int m = lens; m >= i; m--) {
- *(s + m + 1) = *(s + m);
- printf("s = '%s'\n", s);
- }
- *(s + i) = *(sub + k);
- }
- }
- void strins3(char *sub, char *s, int p) {
- int lensub = strlen1(sub);
- int lens = strlen1(s), N = lens;
- if (p > lens)
- p = lens;
- if (p < 1)
- p = 0;
- for (int i = p; i < lensub + p; i++) {
- while (N + 1 != i) {
- s[N + 1] = s[N];
- N--;
- }
- N = lens;
- s[i] = sub[i - p];
- }
- }
- void strins4(char *sub, char *s, int p) {
- int lens = strlen1(s);
- int lensub = strlen1(sub);
- if (p > lens)
- p = lens;
- if (p < 1)
- p = 0;
- int i = p, k = 0;
- begin1:int m = lens;
- begin2:s[m + 1] = s[m];
- m--;
- if (m >= i)
- goto begin2;
- s[i] = sub[k];
- i++, k++;
- if (i < lensub + p)
- goto begin1;
- }
- int strcmp1(char *s1, char *s2) {
- int lens1 = strlen1(s1), lens2 = strlen1(s2), i = 0, s = 0;
- if (lens1 == 0 || lens2 == 0)
- return s;
- while (s1[i] != 0 && s2[i] != 0) {
- if (s1[i] > s2[i]) {
- s = 1;
- break;
- }
- else {
- if (s1[i] < s2[i]) {
- s = -1;
- break;
- }
- }
- i++;
- }
- return s;
- }
- int strcmp2(char *s1, char *s2) {
- while (*s1 && (*s1 == *s2))
- {
- s1++; s2++;
- }
- return *s1 == *s2 ? 0 : *s1 > *s2 ? 1 : -1;
- }
- int strcmp3(char *s1, char *s2) {
- return *s1 > *s2 ? 1 : *s1 < *s2 ? -1 : *s1 ? strcmp3(s1 + 1, s2 + 1) : 0;
- }
- int strcmp4(char *s1, char *s2) {
- int i, lens1(strlen(s1));
- for (i = 0; i < lens1; i++)
- {
- if (s1[i] < s2[i])
- return -1;
- if (s1[i] > s2[i])
- return 1;
- }
- return s2[i] ? -1 : 0;
- }
- void strdel1(char *s, int p, int k) {
- p--;
- int lens = strlen1(s);
- if (p + k > lens)
- k = lens - p;
- int k1 = k;
- for (int j = 0; j < k; j++) {
- for (int i = p + k1; i < strlen1(s); i++) {
- printf("s = '%s'\n", s);
- s[i] = s[i + 1];
- }
- k1--;
- }
- }
- void strdel2(char *s, int p, int k) {
- int lens = strlen1(s);
- if (p + k > lens)
- k = lens - p;
- p--;
- if (p <= lens) {
- while (strlen1(s) != lens - k) {
- for (int i = p + 1; i < lens; i++)
- *(s + i) = *(s + i + 1);
- }
- }
- }
- void strdel3(char *s, int p, int k) {
- int lens = strlen1(s);
- if (p + k > lens)
- k = lens - p;
- int i = p + k - 1;
- begin:if (k > 0) {
- for (int j = i; j < strlen1(s); j++)
- s[j] = s[j + 1];
- printf("s = '%s'\n", s);
- k--;
- i--;
- goto begin;
- }
- }
- void strdel4(char *s, int p, int k) {
- int lens = strlen1(s);
- if (p + k > lens)
- k = lens - p;
- if (k > 0) {
- int N = lens - k;
- char *st = new char[N];
- int pt = p;
- for (int i = 0, j = 0; i < lens; i++) {
- if (i != pt) {
- st[j] = s[i];
- j++;
- }
- else if ((pt - p) < k - 1)
- pt++;
- }
- for (int i = lens - 1; i >= 0; i--)
- s[i] = 0;
- for (int i = 0; i < N; i++) {
- s[i] = st[i];
- }
- }
- }
- int strpos1(char *sub, char *s) {
- int pos = -1, lensub = strlen1(sub), lens = strlen1(s), j = 0;
- if (lensub > lens)
- return -1;
- for (int i = 0; *(s + i) != 0; i++) {
- j = 0;
- if (*(sub + j) == *(s + i)) {
- pos = i + 1;
- for (j = 0; j < lensub; j++, i++) {
- if (*(sub + j) != *(s + i)) {
- pos = -1;
- break;
- }
- if (j == lensub - 1)
- return pos;
- }
- i--;
- }
- }
- return pos;
- }
- int strpos2(char *sub, char *s) {
- int pos = -1, i = 0, j = 0, lens = strlen1(s), lensub = strlen1(sub);
- if (lensub > lens)
- return -1;
- while (s[i] != 0) {
- j = 0;
- if (s[i] == sub[j]) {
- pos = i + 1;
- while (j != lensub) {
- if (s[i] != sub[j]) {
- pos = -1;
- break;
- }
- if (j == lensub - 1)
- return pos;
- j++;
- i++;
- }
- }
- else
- i++;
- }
- return pos;
- }
- int strpos3(char *sub, char *s) {
- int pos = -1;
- int i = 0, j = 0, lensub = strlen1(sub), lens = strlen1(s);
- if (lensub > lens)
- return -1;
- i--;
- begin:
- if (i == lens - 1)
- return -1;
- i++;
- body:if (s[i] == sub[j]) {
- if (pos == -1)
- pos = i + 1;
- if (j == lensub - 1)
- return pos;
- if (s[i + 1] != sub[j + 1]) {
- pos = -1;
- j = 0;
- goto begin;
- }
- j++; i++;
- goto body;
- }
- else
- goto begin;
- }
- int strpos4(char *sub, char *s) {
- int pos = -1, lens = strlen1(s) - 1, len = strlen1(s) - 1, lensub = strlen1(sub);
- int i = 0, j = 0, b = lensub;
- while (len > 0) {
- startiter:if (b == 0)
- return pos;
- if (s[0] == sub[j]) {
- if (pos == -1)
- pos = lens - len + 1;
- j++;
- b--;
- }
- else {
- pos = -1;
- j = 0;
- if (b != lensub) {
- b = lensub;
- goto startiter;
- }
- }
- for (int i = 0; i < len; i++)
- s[i] = s[i + 1];
- len--;
- }
- return pos;
- }
- void testlen(char *s1) {
- cout << "strlen1(\"" << s1 << "\")=: " << strlen1(s1) << endl;
- cout << "strlen2(\"" << s1 << "\")=: " << strlen2(s1) << endl;
- cout << "strlen3(\"" << s1 << "\")=: " << strlen3(s1) << endl;
- cout << "strlen4(\"" << s1 << "\")=: " << strlen4(s1) << endl;
- cout << endl;
- }
- void testcpy(char *dest, char *s, int p, int k) {
- char dest1[256]{ 0 }; int i = 0;
- while (dest[i] != 0) {
- dest1[i] = dest[i];
- i++;
- }
- cout << "strcopy1(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
- cout << "\"" << strcopy1(dest1, s, p, k) << "\"" << endl;
- for (int i = strlen1(dest1) - 1; i >= 0; i--)
- dest1[i] = 0;
- i = 0;
- while (dest[i] != 0) {
- dest1[i] = dest[i];
- i++;
- }
- cout << "strcopy2(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
- cout << "\"" << strcopy2(dest1, s, p, k) << "\"" << endl;
- for (int i = strlen1(dest1) - 1; i >= 0; i--)
- dest1[i] = 0;
- i = 0;
- while (dest[i] != 0) {
- dest1[i] = dest[i];
- i++;
- }
- cout << "strcopy3(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
- cout << "\"" << strcopy3(dest1, s, p, k) << "\"" << endl;
- for (int i = strlen1(dest1) - 1; i >= 0; i--)
- dest1[i] = 0;
- i = 0;
- while (dest[i] != 0) {
- dest1[i] = dest[i];
- i++;
- }
- cout << "strcopy4(\"" << dest1 << "\",\"" << s << "\"," << p << "," << k << ")=: ";
- cout << "\"" << strcopy4(dest1, s, p, k) << "\"" << endl;
- cout << endl;
- }
- void testins(char *sub, char *s, int p) {
- char s1[256]{ 0 }; int i = 0;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- printf("s1 = '%s'\n", s1);
- i = 0;
- cout << "strins1(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
- strins1(sub, s1, p);
- cout << "\"" << s1 << "\"" << endl;
- for (int i = strlen1(s1) - 1; i >= 0; i--)
- s1[i] = 0;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strins2(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
- strins2(sub, s1, p);
- cout << "\"" << s1 << "\"" << endl;
- for (int i = strlen1(s1) - 1; i >= 0; i--)
- s1[i] = 0;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strins3(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
- strins3(sub, s1, p);
- cout << "\"" << s1 << "\"" << endl;
- for (int i = strlen1(s1) - 1; i >= 0; i--)
- s1[i] = 0;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strins4(\"" << sub << "\",\"" << s1 << "," << p << ")=: ";
- strins4(sub, s1, p);
- cout << "\"" << s1 << "\"" << endl;
- cout << endl;
- }
- void testcmp(char *s1, char *s2) {
- cout << "strcmp1(\"" << s1 << "\",\"" << s2 << "\")=: ";
- cout << strcmp1(s1, s2) << endl;
- cout << "strcmp2(\"" << s1 << "\",\"" << s2 << "\")=: ";
- cout << strcmp2(s1, s2) << endl;
- cout << "strcmp3(\"" << s1 << "\",\"" << s2 << "\")=: ";
- cout << strcmp3(s1, s2) << endl;
- cout << "strcmp4(\"" << s1 << "\",\"" << s2 << "\")=: ";
- cout << strcmp4(s1, s2) << endl;
- cout << endl;
- }
- void testdel(char *s, int p, int k) {
- char s1[256]{ 0 }; int i = 0;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strdel1(\"" << s1 << "\"," << p << "," << k << ")=: ";
- strdel1(s1, p, k);
- cout << "\"" << s1 << "\"" << endl;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strdel2(\"" << s1 << "\"," << p << "," << k << ")=: ";
- strdel2(s1, p, k);
- cout << "\"" << s1 << "\"" << endl;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strdel3(\"" << s1 << "\"," << p << "," << k << ")=: ";
- strdel3(s1, p, k);
- cout << "\"" << s1 << "\"" << endl;
- while (s[i] != 0) {
- s1[i] = s[i];
- i++;
- }
- i = 0;
- cout << "strdel4(\"" << s1 << "\"," << p << "," << k << ")=: ";
- strdel4(s1, p, k);
- cout << "\"" << s1 << "\"" << endl;
- cout << endl;
- }
- void teststrpos(char *sub, char *s) {
- cout << "strpos1(\"" << sub << "\",\"" << s << "\")=: ";
- cout << strpos1(sub, s) << endl;
- cout << "strpos2(\"" << sub << "\",\"" << s << "\")=: ";
- cout << strpos2(sub, s) << endl;
- cout << "strpos3(\"" << sub << "\",\"" << s << "\")=: ";
- cout << strpos3(sub, s) << endl;
- cout << "strpos4(\"" << sub << "\",\"" << s << "\")=: ";
- cout << strpos4(sub, s) << endl;
- cout << endl;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- menu:while (true) {
- clr;
- cin.clear();
- cout << "1.strlen\n2.strcopy\n3.strins\n4.strcmp\n5.strdel\n6.strpos\n\n\n0.Выход";
- switch (_getch()) {
- case '1': {
- clr;
- char s1[] = "hello";
- testlen(s1);
- char s2[] = "";
- testlen(s2);
- char s3[] = "012";
- testlen(s3);
- char s4[] = "-12345";
- testlen(s4);
- pause;
- goto menu;
- }
- case '2': {
- clr;
- int p, k;
- char dest1[25] = "rty";
- char s1[] = "012345";
- p = 0;
- k = 3;
- testcpy(dest1, s1, p, k);
- char s2[] = "012345";
- p = 20;
- k = 1;
- char dest2[] = "";
- testcpy(dest2, s2, p, k);
- char s3[] = "012345";
- p = 4;
- k = 10;
- char dest3[] = "";
- testcpy(dest3, s3, p, k);
- char s4[] = "0helloboom343";
- p = 2;
- k = 8;
- char dest4[] = "";
- testcpy(dest4, s4, p, k);
- pause;
- goto menu;
- }
- case '3': {
- clr;
- int p;
- char sub1[] = "ab", s1[] = "012345678";
- p = 0;
- testins(sub1, s1, p);
- char sub2[] = "ab", s2[] = "012345678";
- p = 3;
- testins(sub2, s2, p);
- char sub3[] = "ab", s3[] = "012345678";
- p = 10;
- testins(sub3, s3, p);
- char sub4[] = "ab", s4[] = "012345678";
- p = 0;
- testins(sub4, s4, p);
- pause;
- goto menu;
- }
- case '4': {
- clr;
- char s11[] = "hello", s12[] = "hellor";
- testcmp(s11, s12);
- char s21[] = "hello", s22[] = "boomboom";
- testcmp(s21, s22);
- char s31[] = "bello", s32[] = "booms";
- testcmp(s31, s32);
- char s41[] = "", s42[] = "";
- testcmp(s41, s42);
- pause;
- goto menu;
- }
- case '5': {
- clr;
- int p, k;
- char s1[] = "012345";
- p = 2; k = 30;
- testdel(s1, p, k);
- char s2[] = "012345";
- p = 20; k = 3;
- testdel(s2, p, k);
- char s3[] = "012345abcde";
- p = 0; k = 6;
- testdel(s3, p, k);
- char s4[] = "012345";
- p = 1; k = 3;
- testdel(s4, p, k);
- pause;
- goto menu;
- }
- case '6': {
- clr;
- char sub1[] = "345", s1[] = "0123456";
- teststrpos(sub1, s1);
- char sub2[] = "123", s2[] = "012123";
- teststrpos(sub2, s2);
- char sub3[] = "55", s3[] = "444545544";
- teststrpos(sub3, s3);
- char sub4[] = "ab", s4[] = "acdeabcde";
- teststrpos(sub4, s4);
- pause;
- goto menu;
- }
- case '0': exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement