Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- си строка
- 1 функция - maxlen длина строки
- 2 функция - mycopy ( mycopy(a, b), где a - с какого, b - сколько)
- */
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- #include <cmath>
- #include <time.h>
- #define NUMS_CHAR_LEN 10
- // ввод строки
- char *get_string(int *len) {
- *len = 0;
- int capacity = 1;
- char *s = (char*) malloc(sizeof(char));
- char c = getchar();
- while (c != '\n') {
- s[(*len)++] = c;
- if (*len >= capacity) {
- capacity += 1;
- s = (char*) realloc(s, capacity * sizeof(char));
- }
- c = getchar();
- }
- s[*len] = '\0';
- return s;
- }
- // узнать длинну строки
- int maxlen(char* str) {
- int i = 0;
- while (true) {
- if (str[i] == '\0') return i;
- i++;
- }
- }
- // вырезать часть строки
- char* mycopy(char* str, int a, int b) {
- char *s = (char*) malloc((b - a) * sizeof(char));
- for (int i = 0; i < (b - a); i++) {
- s[i] = str[a + i];
- }
- return s;
- }
- int main() {
- // смена кодировки
- system("chcp 65001"); // можно заменить на setlocale(LC_ALL, "Russian");
- int len;
- cout << "Введите строку: ";
- char* str = get_string(&len);
- cout << "Введённая строка: " << str << endl;
- cout << "Длинна строки: " << maxlen(str) << endl;
- int a, b;
- // вводим диапазон чисел
- while (true) {
- cout << "A = ";
- scanf("%d", &a);
- cout << "B = ";
- scanf("%d", &b);
- // проверка
- if (a >= 0 && b <= maxlen(str))
- break;
- else
- cout << "Вы вышли за границы строки." << endl;
- }
- // вывод вырезанной строки
- cout << "Вырезанная строка: " << mycopy(str, a, b) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment