How to count the number of non-overlapping instances of a substring in a string in C#

3 Answers

0 votes
using System;
 
public class CountNumberOfNonOverlappingInstancesOfSubstringInAString_CSharp
{
    public static int countOccurrences(string str, string substr) {
        if (substr.Length == 0) {
            return 0;
        }
 
        int count = 0;
        int offset = str.IndexOf(substr, StringComparison.Ordinal);
        while (offset != -1) {
            count++;
            offset = str.IndexOf(substr, offset + substr.Length, StringComparison.Ordinal);
        }
 
        return count;
    }
 
    public static void Main(string[] args)
    {
        string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
 
        Console.WriteLine(countOccurrences(s, "php"));
    }
}
 


/*
run:
     
6
        
*/

 



answered Aug 24, 2024 by avibootz
edited 6 hours ago by avibootz
0 votes
using System;
 
public class CountNumberOfNonOverlappingInstancesOfSubstringInAString_CSharp
{
    public static int countOccurrences(string str, string substr) {
        return (str.Length - str.Replace(substr, String.Empty).Length) / substr.Length;
    }
 
    public static void Main(string[] args)
    {
        string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
 
        Console.WriteLine(countOccurrences(s, "php"));
    }
}
 
  
/*
run:
     
6
        
*/
 

 



answered Aug 24, 2024 by avibootz
edited 6 hours ago by avibootz
0 votes
using System;

class Program
{
    // Non‑overlapping occurrences are matches of a substring that do not reuse any of
    // the same characters. Once one match is counted, the next search must begin
    // after that match ends.

    static int count_non_overlapping(string haystack, string needle)
    {
        /*
           Count how many times 'needle' appears in 'haystack' without overlapping.
           The algorithm:
             • Use String.IndexOf() to locate the next occurrence.
             • Each time a match is found, move the search index forward
               by the full length of the matched substring.
             • This ensures no characters are reused between matches.
        */

        int count = 0;
        int index = 0;  // current search position in the main string

        // Continue searching until IndexOf() returns -1 (meaning: no more matches)
        while (true)
        {
            // Find the next occurrence starting at the current index
            int pos = haystack.IndexOf(needle, index);

            if (pos == -1) {
                // No more matches found
                break;
            }

            // We found a match, so increment the count
            count++;

            // Move index forward by the length of the needle
            // This ensures the next search begins *after* the matched substring
            index = pos + needle.Length;
        }

        return count;
    }


    // ---------------------------------------------------------------
    static void Main()
    {
        // Demonstration using the string provided in the instructions:
        string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
        string substring = "php";

        // Count non-overlapping occurrences
        int result = count_non_overlapping(s, substring);

        Console.WriteLine("Non-overlapping occurrences: " + result);
    }
}



/*
run:

Non-overlapping occurrences: 6

*/

 



answered 6 hours ago by avibootz

Related questions

...