Advertisement
GarikK

String Tasks

Apr 22nd, 2020
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  test
  4. //
  5. //  Created by MacBook on 18.04.2020.
  6. //  Copyright © 2020 MacBook. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. void allEnteringSymbols()
  13. {
  14.     //deleting all entering symbols
  15.     char baby[32];
  16.     cout << "Please enter a string" << endl;
  17.     cin >> baby;
  18.     char symbolToDelete;
  19.     cout << "What symbol to delete? Enter please a number " << endl;
  20.     cin >> symbolToDelete;
  21.     int stringCounter = 0;
  22.     while (*(baby + stringCounter) != '\0') {
  23.         stringCounter++;
  24.     }
  25.    
  26.     for (int i = 0; i < stringCounter; i++) {
  27.         if(baby[i] == symbolToDelete)
  28.         {
  29.             baby[i] = '\0';
  30.         }
  31.     }
  32.    
  33.     for (int i = 0; i < stringCounter; i++) {
  34.         cout << baby[i] << endl;
  35.     }
  36. }
  37. //--------------
  38. //symbol to delete
  39. void symbolToDelete()
  40. {
  41.   char string[32];
  42.   int numberToDelete = 0;
  43.   cout << "Please enter a string" << endl;
  44.   cin >> string;
  45.   cout << "Please enter what symbol to delete? Enter a number" << endl;
  46.   cin >> numberToDelete;
  47.   string[numberToDelete] = ' ';
  48.   cout << string;
  49. }
  50. //entering a symbol to string by a number
  51. void enteringSymbol ()
  52. {
  53.     char string[] = "Hello World";
  54.     int positionNumber = 0;
  55.     char symbolToChange;
  56.     cout << "Please enter the position number" << endl;
  57.     cin >> positionNumber;
  58.     cout << "Please enter a symbol to change" << endl;
  59.     cin >> symbolToChange;
  60.     string[positionNumber] = symbolToChange;
  61.     cout << string << endl;
  62.    
  63. }
  64.  
  65. void changingSymbols ()
  66. {
  67.     char string[32];
  68.     char symbolToChange = '!';
  69.     int stringCounter = 0;
  70.     cout << "Please enter a string" << endl;
  71.     cin >> string;
  72.     while (string[stringCounter] != '\0') {
  73.         if (string[stringCounter] == '.')
  74.         {
  75.             string[stringCounter] = symbolToChange;
  76.         }
  77.         stringCounter++;
  78.     }
  79.     cout << string << endl;
  80. }
  81.  
  82. void symbolCounter()
  83. {
  84.     char string[32];
  85.     char symbolToRescue;
  86.     int symbolCounter = 0;
  87.     cout << "Please enter a string" << endl;
  88.     cin >> string;
  89.     cout << "Please enter the symbol to find" << endl;
  90.     cin >> symbolToRescue;
  91.     for (int i = 0; i < strlen(string); i++) {
  92.         if (string[i] == symbolToRescue) {
  93.             symbolCounter++;
  94.         }
  95.     }
  96.     cout << "How many times the symbol in the string -> " << symbolCounter << endl;
  97.    
  98. }
  99. //получаем длину строки
  100. int GetStringSize(char *str)
  101. {
  102.     int counter = 0;
  103.     while (str[counter] != '\0') {
  104.         counter++;
  105.     }
  106.     return counter;
  107. }
  108. //функция проверки на буквы
  109. bool IsCharSymbol(char Symbol)
  110. {
  111.     short symb = Symbol;
  112.     if (((symb > 64) && (symb < 91)) || ((symb > 96) && (symb < 123))) {
  113.         return true;
  114.     }
  115.     return false;
  116. }
  117. //функция проверки на цифры
  118. bool IsCharNumber(char Number)
  119. {
  120.     short symb = Number;
  121.     if ( symb > 47 && symb < 58) {
  122.         return true;
  123.     }
  124.     return false;
  125. }
  126.  
  127. int main(int argc, const char * argv[]) {
  128.    
  129.     allEnteringSymbols();
  130.     enteringSymbol();
  131.     changingSymbols();
  132.     symbolCounter();
  133.     char* str = new char [128];
  134.     cin.getline(str, 128);
  135.    
  136.     int StringLength = GetStringSize(str);//получили длину
  137.     int LeterCounter = 0;
  138.     int NumberCounter = 0;
  139.     int OtherSymbols = 0;
  140.     for (int i = 0; i < StringLength; i++) {//идем по стрингу
  141.         if (IsCharSymbol(str[i])) {
  142.             LeterCounter++;
  143.         }else if(IsCharNumber(str[i]))
  144.         {
  145.             NumberCounter++;
  146.         } else{
  147.             OtherSymbols++;
  148.         }
  149.     }
  150.     cout << "Letter quantity is " << LeterCounter << endl;
  151.     cout << "Number quantity is " << NumberCounter << endl;
  152.     cout << "OtherSymbols quantity is " << OtherSymbols << endl;
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement