Advertisement
yahorrr

Untitled

May 14th, 2022
765
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2.         /// Searches a string for all characters that are in <see cref="Array" />, and returns the number of occurrences of all characters.
  3.         /// </summary>
  4.         /// <param name="str">String to search.</param>
  5.         /// <param name="chars">One-dimensional, zero-based <see cref="Array"/> that contains characters to search for.</param>
  6.         /// <returns>The number of occurrences of all characters.</returns>
  7.         public static int GetCharsCount(string str, char[] chars)
  8.         {
  9.             // #1. Implement the method using "for" statement.
  10.             if (chars is null)
  11.             {
  12.                 throw new ArgumentNullException(nameof(chars));
  13.             }
  14.  
  15.             if (str is null)
  16.             {
  17.                 throw new ArgumentNullException(nameof(str));
  18.             }
  19.  
  20.             int counter = 0;
  21.  
  22.             for (int i = 0;  i < chars.Length; i++)
  23.             {
  24.                 for (int k = 0; k < str.Length; k++)
  25.                 {
  26.                     if (chars[i] == str[k])
  27.                     {
  28.                         counter++;
  29.                     }
  30.                 }
  31.             }
  32.  
  33.             return counter;
  34.         }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement