How to check if two strings have the same number of words in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static bool two_strings_have_same_number_of_words(string str1, string str2) {
        List<string> wordsOfStr1 = str1.Split(" ").ToList();
        List<string> wordsOfStr2 = str2.Split(" ").ToList();

        return wordsOfStr1.Count == wordsOfStr2.Count;
    }

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

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


 
 
/*
run:
 
yes

*/

 



answered May 9, 2024 by avibootz
...