Advertisement
yahorrr

Untitled

May 14th, 2022
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. using System;
  2.  
  3. namespace LookingForChars
  4. {
  5.     public static class CharsCounter
  6.     {
  7.         /// <summary>
  8.         /// Searches a string for all characters that are in <see cref="Array" />, and returns the number of occurrences of all characters.
  9.         /// </summary>
  10.         /// <param name="str">String to search.</param>
  11.         /// <param name="chars">One-dimensional, zero-based <see cref="Array"/> that contains characters to search for.</param>
  12.         /// <returns>The number of occurrences of all characters.</returns>
  13.         public static int GetCharsCount(string str, char[] chars)
  14.         {
  15.             // #1. Implement the method using "for" statement.
  16.             if (chars is null)
  17.             {
  18.                 throw new ArgumentNullException(nameof(chars));
  19.             }
  20.  
  21.             if (str is null)
  22.             {
  23.                 throw new ArgumentNullException(nameof(str));
  24.             }
  25.  
  26.             int counter = 0;
  27.  
  28.             for (int i = 0;  i < str.Length; i++)
  29.             {
  30.                 for (int k = 0; k < chars.Length; k++)
  31.                 {
  32.                     if (str[i] == chars[k])
  33.                     {
  34.                         counter++;
  35.                         break;
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             return counter;
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Searches a string for all characters that are in <see cref="Array" />, and returns the number of occurrences of all characters within the range of elements in the <see cref="string"/> that starts at the specified index and ends at the specified index.
  45.         /// </summary>
  46.         /// <param name="str">String to search.</param>
  47.         /// <param name="chars">One-dimensional, zero-based <see cref="Array"/> that contains characters to search for.</param>
  48.         /// <param name="startIndex">A zero-based starting index of the search.</param>
  49.         /// <param name="endIndex">A zero-based ending index of the search.</param>
  50.         /// <returns>The number of occurrences of all characters within the specified range of elements in the <see cref="string"/>.</returns>
  51.         public static int GetCharsCount(string str, char[] chars, int startIndex, int endIndex)
  52.         {
  53.             // #2. Implement the method using "while" statement.
  54.             if (chars is null)
  55.             {
  56.                 throw new ArgumentNullException(nameof(chars));
  57.             }
  58.  
  59.             if (str is null)
  60.             {
  61.                 throw new ArgumentNullException(nameof(str));
  62.             }
  63.  
  64.             if (startIndex < 0)
  65.             {
  66.                 throw new ArgumentOutOfRangeException(nameof(startIndex));
  67.             }
  68.  
  69.             if (endIndex < 0)
  70.             {
  71.                 throw new ArgumentOutOfRangeException(nameof(endIndex));
  72.             }
  73.  
  74.             if (startIndex > endIndex)
  75.             {
  76.                 throw new ArgumentException("Start index greater then end index", nameof(startIndex));
  77.             }
  78.  
  79.             if (startIndex > str.Length)
  80.             {
  81.                 throw new ArgumentOutOfRangeException(nameof(startIndex));
  82.             }
  83.  
  84.             if (endIndex > str.Length)
  85.             {
  86.                 throw new ArgumentOutOfRangeException(nameof(endIndex));
  87.             }
  88.  
  89.             int i = startIndex;
  90.             int k = 0;
  91.             int counter = 0;
  92.  
  93.             while (i <= endIndex)
  94.             {
  95.                 k = 0;
  96.  
  97.                 while (k < chars.Length)
  98.                 {
  99.                     if (str[i] == chars[k])
  100.                     {
  101.                         counter++;
  102.                         break;
  103.                     }
  104.  
  105.                     k++;
  106.                 }
  107.  
  108.                 i++;
  109.             }
  110.  
  111.             return counter;
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Searches a string for a limited number of characters that are in <see cref="Array" />, and returns the number of occurrences of all characters within the range of elements in the <see cref="string"/> that starts at the specified index and ends at the specified index.
  116.         /// </summary>
  117.         /// <param name="str">String to search.</param>
  118.         /// <param name="chars">One-dimensional, zero-based <see cref="Array"/> that contains characters to search for.</param>
  119.         /// <param name="startIndex">A zero-based starting index of the search.</param>
  120.         /// <param name="endIndex">A zero-based ending index of the search.</param>
  121.         /// <param name="limit">A maximum number of characters to search.</param>
  122.         /// <returns>The limited number of occurrences of characters to search for within the specified range of elements in the <see cref="string"/>.</returns>
  123.         public static int GetCharsCount(string str, char[] chars, int startIndex, int endIndex, int limit)
  124.         {
  125.             // #3. Implement the method using "do..while" statements.
  126.             if (chars is null)
  127.             {
  128.                 throw new ArgumentNullException(nameof(chars));
  129.             }
  130.  
  131.             if (str is null)
  132.             {
  133.                 throw new ArgumentNullException(nameof(str));
  134.             }
  135.  
  136.             if (startIndex < 0)
  137.             {
  138.                 throw new ArgumentOutOfRangeException(nameof(startIndex));
  139.             }
  140.  
  141.             if (endIndex < 0)
  142.             {
  143.                 throw new ArgumentOutOfRangeException(nameof(endIndex));
  144.             }
  145.  
  146.             if (startIndex > endIndex)
  147.             {
  148.                 throw new ArgumentException("Start index greater then end index", nameof(startIndex));
  149.             }
  150.  
  151.             if (startIndex > str.Length)
  152.             {
  153.                 throw new ArgumentOutOfRangeException(nameof(startIndex));
  154.             }
  155.  
  156.             if (endIndex > str.Length)
  157.             {
  158.                 throw new ArgumentOutOfRangeException(nameof(endIndex));
  159.             }
  160.  
  161.             int i = startIndex;
  162.             int k = 0;
  163.             int counter = 0;
  164.  
  165.             if (str.Length == 0)
  166.             {
  167.                 return 0;
  168.             }
  169.  
  170.             if (chars.Length == 0)
  171.             {
  172.                 return 0;
  173.             }
  174.  
  175.             do
  176.             {
  177.                 k = 0;
  178.                 do
  179.                 {
  180.                     if (str[i] == chars[k])
  181.                     {
  182.                         counter++;
  183.                     }
  184.  
  185.                     k++;
  186.                 }
  187.                 while (k < chars.Length);
  188.  
  189.                 i++;
  190.             }
  191.             while (i <= endIndex && counter <= limit);
  192.  
  193.             return counter;
  194.         }
  195.     }
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement