Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. //main.cpp
  2.  
  3. #include <iostream>
  4. #include "functions.h"
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     const int size = 4;
  10.     char set[size] = { 'e', 't', 'i', 's' };
  11.     char * string = new char[100];
  12.     int counter = 0;
  13.     cout << "Enter your string: " << endl;
  14.     cin.getline(string, 100);
  15.  
  16.     while (string[counter] != '\0')
  17.     {
  18.         counter++;
  19.     }
  20.     maxOccurancesInString(set, string, size, counter);
  21.     return 0;
  22. }
  23.  
  24.  
  25. //functions.cpp
  26.  
  27. #include <iostream>
  28. #include "functions.h"
  29.  
  30. int maxOccurancesInString(char * set, char *string, unsigned setSize, unsigned stringSize)
  31. {
  32.     int occurs = 0;
  33.     int * occurances = new int[setSize];
  34.     int counter = 0;
  35.  
  36.     for (size_t i = 0; i < setSize; i++)
  37.     {
  38.         occurs = 0;
  39.         for (size_t k = 0; k < stringSize; k++)
  40.         {
  41.             if (set[i] == string[k])
  42.             {
  43.                 occurs++;
  44.             }
  45.             if (k == stringSize - 1)
  46.             {
  47.                 occurances[counter] = occurs;
  48.                 counter++;
  49.             }
  50.         }
  51.     }
  52.     int max = occurances[0];
  53.     int positionMax = 0;
  54.     for (int m = 0; m < counter; m++)
  55.     {
  56.         if (occurances[m] > max)
  57.         {
  58.             max = occurances[m];
  59.             positionMax = m;
  60.         }
  61.     }
  62.     std::cout << set[positionMax] <<  " - " << max << "\n";
  63.     return 0;
  64. }
  65.  
  66.  
  67. //functions.h
  68.  
  69. #ifndef _H_FUNCTIONS_H_
  70. #define _H_FUNCTIONS_H_
  71.  
  72. int maxOccurancesInString(char *, char *, unsigned, unsigned);
  73.  
  74. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement