Advertisement
romik1505

Untitled

Mar 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include "stdio.h"
  4. #include <iostream>
  5. #include "stdlib.h"
  6. #include "windows.h"
  7. #include "string.h"
  8.  
  9. #define MAX_STRING_LENGHT 1024
  10.  
  11. /*
  12.     1.  Разработать программу, которая определяет длину строки, хранящейся в переменной типа string.
  13. */
  14.  
  15. using namespace std;
  16.  
  17. void getstring(string str);
  18. void printstring(string str);
  19. int str_len(string str);
  20.  
  21.  
  22. int main()
  23. {
  24.     SetConsoleCP(1251);
  25.     SetConsoleOutputCP(1251);
  26.  
  27.     string str;
  28.  
  29.     printf("Введите строку\n");
  30.     getstring(str);
  31.     printstring(str);
  32.     printf("\nДлина строки %d\n", str_len(str));
  33.  
  34.     system("pause");
  35.     return 0;
  36. }
  37.  
  38.  
  39. // Посимвольный ввод строки
  40. void getstring(string str)
  41. {
  42.     int i = 0;
  43.     do
  44.     {
  45.         str += (char)getchar();
  46.         i++;
  47.     }while((str[i] != '\n')&&(str[i] != '\0') && (i < MAX_STRING_LENGHT)) ;
  48.     str[i] = '\0';
  49. }
  50.  
  51. // Посимвольный вывод строки
  52. void printstring(string str)
  53. {
  54.     for (int i = 0; (i < MAX_STRING_LENGHT) && (str[i] != '\0'); i++)   putchar(str[i]);
  55. }
  56.  
  57. // Подсчет длины строки (количество символов до нуль символа)
  58. int str_len(string str)
  59. {
  60.     int i = 0;
  61.     while ((str[i] != '\0') && (str[i] != '\n') && (i < MAX_STRING_LENGHT)) i++;
  62.     printf("-%d-", i);
  63.     return i;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement