text: Check if two strings are same ignoring their cases | Function to compare two strings ignoring their cases ; length of first string ; length of second string ; if length is not same simply return false since both string can not be same if length is not equal ; loop to match one by one all characters of both string ; if current characters of both string are same , increase value of i to compare next character ; if any character of first string is some special character or numeric character and not same as corresponding character of second string then return false ; do the same for second string ; this block of code will be executed if characters of both strings are of different cases ; compare characters by ASCII value ; if characters matched , increase the value of i to compare next char ; if all characters of the first string are matched with corresponding characters of the second string , then return true ; Function to print the same or not same if strings are equal or not equal ; Driver Code
code: using System ; class GFG { static bool equalIgnoreCase ( string str1 , string str2 ) { int i = 0 ; int len1 = str1 . Length ; int len2 = str2 . Length ; if ( len1 != len2 ) return false ; while ( i < len1 ) { if ( str1 [ i ] == str2 [ i ] ) { i ++ ; } else if ( ! ( ( str1 [ i ] >= ' a ' && str1 [ i ] <= ' z ' ) || ( str1 [ i ] >= ' A ' && str1 [ i ] <= ' Z ' ) ) ) { return false ; } else if ( ! ( ( str2 [ i ] >= ' a ' && str2 [ i ] <= ' z ' ) || ( str2 [ i ] >= ' A ' && str2 [ i ] <= ' Z ' ) ) ) { return false ; } else { if ( str1 [ i ] >= ' a ' && str1 [ i ] <= ' z ' ) { if ( str1 [ i ] - 32 != str2 [ i ] ) return false ; } else if ( str1 [ i ] >= ' A ' && str1 [ i ] <= ' Z ' ) { if ( str1 [ i ] + 32 != str2 [ i ] ) return false ; } i ++ ; return true ; static void equalIgnoreCaseUtil ( string str1 , string str2 ) { bool res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) Console . WriteLine ( " Same " ) ; else Console . WriteLine ( " Not ▁ Same " ) ; } public static void Main ( ) { string str1 , str2 ; str1 = " Geeks " ; str2 = " geeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " Geek " ; str2 = " geeksforgeeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; } }