Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Searches a string for all characters that are in <see cref="Array" />, and returns the number of occurrences of all characters.
- /// </summary>
- /// <param name="str">String to search.</param>
- /// <param name="chars">One-dimensional, zero-based <see cref="Array"/> that contains characters to search for.</param>
- /// <returns>The number of occurrences of all characters.</returns>
- public static int GetCharsCount(string str, char[] chars)
- {
- // #1. Implement the method using "for" statement.
- if (chars is null)
- {
- throw new ArgumentNullException(nameof(chars));
- }
- if (str is null)
- {
- throw new ArgumentNullException(nameof(str));
- }
- int counter = 0;
- for (int i = 0; i < chars.Length; i++)
- {
- for (int k = 0; k < str.Length; k++)
- {
- if (chars[i] == str[k])
- {
- counter++;
- }
- }
- }
- return counter;
- }
Advertisement
Advertisement
Advertisement
RAW Paste Data
Copied
Advertisement