Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "stdio.h"
- #include <iostream>
- #include "stdlib.h"
- #include "windows.h"
- #include "string.h"
- #define MAX_STRING_LENGHT 1024
- /*
- 1. Разработать программу, которая определяет длину строки, хранящейся в переменной типа string.
- */
- using namespace std;
- void getstring(string str);
- void printstring(string str);
- int str_len(string str);
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string str;
- printf("Введите строку\n");
- getstring(str);
- printstring(str);
- printf("\nДлина строки %d\n", str_len(str));
- system("pause");
- return 0;
- }
- // Посимвольный ввод строки
- void getstring(string str)
- {
- int i = 0;
- do
- {
- str += (char)getchar();
- i++;
- }while((str[i] != '\n')&&(str[i] != '\0') && (i < MAX_STRING_LENGHT)) ;
- str[i] = '\0';
- }
- // Посимвольный вывод строки
- void printstring(string str)
- {
- for (int i = 0; (i < MAX_STRING_LENGHT) && (str[i] != '\0'); i++) putchar(str[i]);
- }
- // Подсчет длины строки (количество символов до нуль символа)
- int str_len(string str)
- {
- int i = 0;
- while ((str[i] != '\0') && (str[i] != '\n') && (i < MAX_STRING_LENGHT)) i++;
- printf("-%d-", i);
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement