You must Sign In to post a response.
  • Category: .NET

    How to check between two common strings one vowel missing and one consonant is different

    how to check between two common strings one vowel missing and one consonant is different
    and rest of the letters are same.

    please help

    ex :- string1 : ABCDEFGH string2 : ABCDFGK
  • #770435
    Hi Chanti

    please try below code

    static void Main()
    {
    string string1 = "ABCDEFGH";
    string string2 = "ABCDFGK";

    bool result = CheckStrings(string1, string2);
    Console.WriteLine(result ? "Strings match the criteria." : "Strings do not match the criteria.");
    }

    static bool CheckStrings(string str1, string str2)
    {
    // Check if lengths match the required criteria
    if (Math.Abs(str1.Length - str2.Length) != 1)
    return false;

    // Identify missing vowel and different consonant
    int vowelCount1 = 0, vowelCount2 = 0;
    char differentChar = '\0';
    bool foundDifference = false;

    int minLength = Math.Min(str1.Length, str2.Length);

    for (int i = 0; i < minLength; i++)
    {
    if (str1[i] != str2[i])
    {
    // If we already found a difference, it's invalid
    if (foundDifference)
    return false;

    differentChar = str1[i] != str2[i] ? str2[i] : str1[i];
    foundDifference = true;
    }

    if (IsVowel(str1[i])) vowelCount1++;
    if (IsVowel(str2[i])) vowelCount2++;
    }

    // Handle the extra character in the longer string
    if (str1.Length > str2.Length)
    {
    if (IsVowel(str1[minLength])) vowelCount1++;
    }
    else
    {
    if (IsVowel(str2[minLength])) vowelCount2++;
    }

    // Check if we have one vowel missing and one consonant different
    return (Math.Abs(vowelCount1 - vowelCount2) == 1) && foundDifference;
    }

    static bool IsVowel(char c)
    {
    return "AEIOUaeiou".IndexOf(c) >= 0;
    }


  • Sign In to post your comments