Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace LookingForChars
- {
- public static class CharsCounter
- {
- /// <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 < str.Length; i++)
- {
- for (int k = 0; k < chars.Length; k++)
- {
- if (str[i] == chars[k])
- {
- counter++;
- break;
- }
- }
- }
- return counter;
- }
- /// <summary>
- /// 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.
- /// </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>
- /// <param name="startIndex">A zero-based starting index of the search.</param>
- /// <param name="endIndex">A zero-based ending index of the search.</param>
- /// <returns>The number of occurrences of all characters within the specified range of elements in the <see cref="string"/>.</returns>
- public static int GetCharsCount(string str, char[] chars, int startIndex, int endIndex)
- {
- // #2. Implement the method using "while" statement.
- if (chars is null)
- {
- throw new ArgumentNullException(nameof(chars));
- }
- if (str is null)
- {
- throw new ArgumentNullException(nameof(str));
- }
- if (startIndex < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(startIndex));
- }
- if (endIndex < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(endIndex));
- }
- if (startIndex > endIndex)
- {
- throw new ArgumentException("Start index greater then end index", nameof(startIndex));
- }
- if (startIndex > str.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(startIndex));
- }
- if (endIndex > str.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(endIndex));
- }
- int i = startIndex;
- int k = 0;
- int counter = 0;
- while (i <= endIndex)
- {
- k = 0;
- while (k < chars.Length)
- {
- if (str[i] == chars[k])
- {
- counter++;
- break;
- }
- k++;
- }
- i++;
- }
- return counter;
- }
- /// <summary>
- /// 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.
- /// </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>
- /// <param name="startIndex">A zero-based starting index of the search.</param>
- /// <param name="endIndex">A zero-based ending index of the search.</param>
- /// <param name="limit">A maximum number of characters to search.</param>
- /// <returns>The limited number of occurrences of characters to search for within the specified range of elements in the <see cref="string"/>.</returns>
- public static int GetCharsCount(string str, char[] chars, int startIndex, int endIndex, int limit)
- {
- // #3. Implement the method using "do..while" statements.
- if (chars is null)
- {
- throw new ArgumentNullException(nameof(chars));
- }
- if (str is null)
- {
- throw new ArgumentNullException(nameof(str));
- }
- if (startIndex < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(startIndex));
- }
- if (endIndex < 0)
- {
- throw new ArgumentOutOfRangeException(nameof(endIndex));
- }
- if (startIndex > endIndex)
- {
- throw new ArgumentException("Start index greater then end index", nameof(startIndex));
- }
- if (startIndex > str.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(startIndex));
- }
- if (endIndex > str.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(endIndex));
- }
- int i = startIndex;
- int k = 0;
- int counter = 0;
- if (str.Length == 0)
- {
- return 0;
- }
- if (chars.Length == 0)
- {
- return 0;
- }
- do
- {
- k = 0;
- do
- {
- if (str[i] == chars[k])
- {
- counter++;
- }
- k++;
- }
- while (k < chars.Length);
- i++;
- }
- while (i <= endIndex && counter <= limit);
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement