Advertisement
yahorrr

Untitled

May 14th, 2022
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.51 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 = 0;
  90.             int k = startIndex;
  91.             int counter = 0;
  92.  
  93.             while (k <= endIndex)
  94.             {
  95.                 while (i < chars.Length)
  96.                 {
  97.                     if (chars[i] == str[k])
  98.                     {
  99.                         counter++;
  100.                         break;
  101.                     }
  102.  
  103.                     i++;
  104.                 }
  105.  
  106.                 k++;
  107.             }
  108.  
  109.             return counter;
  110.         }
  111.  
  112.         /// <summary>
  113.         /// 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.
  114.         /// </summary>
  115.         /// <param name="str">String to search.</param>
  116.         /// <param name="chars">One-dimensional, zero-based <see cref="Array"/> that contains characters to search for.</param>
  117.         /// <param name="startIndex">A zero-based starting index of the search.</param>
  118.         /// <param name="endIndex">A zero-based ending index of the search.</param>
  119.         /// <param name="limit">A maximum number of characters to search.</param>
  120.         /// <returns>The limited number of occurrences of characters to search for within the specified range of elements in the <see cref="string"/>.</returns>
  121.         public static int GetCharsCount(string str, char[] chars, int startIndex, int endIndex, int limit)
  122.         {
  123.             // #3. Implement the method using "do..while" statements.
  124.             if (chars is null)
  125.             {
  126.                 throw new ArgumentNullException(nameof(chars));
  127.             }
  128.  
  129.             if (str is null)
  130.             {
  131.                 throw new ArgumentNullException(nameof(str));
  132.             }
  133.  
  134.             if (startIndex < 0)
  135.             {
  136.                 throw new ArgumentOutOfRangeException(nameof(startIndex));
  137.             }
  138.  
  139.             if (endIndex < 0)
  140.             {
  141.                 throw new ArgumentOutOfRangeException(nameof(endIndex));
  142.             }
  143.  
  144.             if (startIndex > endIndex)
  145.             {
  146.                 throw new ArgumentException("Start index greater then end index", nameof(startIndex));
  147.             }
  148.  
  149.             if (startIndex > str.Length)
  150.             {
  151.                 throw new ArgumentOutOfRangeException(nameof(startIndex));
  152.             }
  153.  
  154.             if (endIndex > str.Length)
  155.             {
  156.                 throw new ArgumentOutOfRangeException(nameof(endIndex));
  157.             }
  158.  
  159.             int i = 0;
  160.             int k = startIndex;
  161.             int counter = 0;
  162.  
  163.             if (str.Length == 0)
  164.             {
  165.                 return 0;
  166.             }
  167.  
  168.             if (chars.Length == 0)
  169.             {
  170.                 return 0;
  171.             }
  172.  
  173.             do
  174.             {
  175.                 do
  176.                 {
  177.                     if (str[k] == chars[i])
  178.                     {
  179.                         counter++;
  180.                     }
  181.  
  182.                     i++;
  183.                 }
  184.                 while (i < chars.Length );
  185.  
  186.                 k++;
  187.             }
  188.             while (k <= endIndex && counter <= limit);
  189.  
  190.             return counter;
  191.         }
  192.     }
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement