Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to check if two equal-length strings are at least 50% equal in C#

1 Answer

0 votes
using System;

public class Program
{
    public static bool Are50PercentEqual(string str1, string str2) {
        if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2) || str1.Length != str2.Length) {
            return false;
        }

        int matchingChars = 0;

        for (int i = 0; i < str1.Length; i++) {
            if (str1[i] == str2[i]) {
                matchingChars++;
            }
        }

        return (double)matchingChars / str1.Length >= 0.5;
    }

    public static void Main(string[] args)
    {
        string str1 = "java c# c c++ python";
        string str2 = "java c# c r rust sql";

        if (Are50PercentEqual(str1, str2)) {
            Console.WriteLine("yes");
        }
        else {
            Console.WriteLine("no");
        }
    }
}



/*
run:

yes

*/

 



answered May 10, 2024 by avibootz

Related questions

...