Computer Applications

Write a function that checks whether or not two arrays (of characters) are identical, that is, whether they have same characters and all characters in corresponding positions are equal.

Java

Java Arrays

4 Likes

Answer

public class KboatArrCompare
{
    public void arrCompare (char A[], char B[])
    {
        int l1 = A.length;
        int l2 = B.length;
        boolean flag = true;
        if (l1 == l2)   {
            for(int i = 0; i < l1; i++)  {
                if(A[i] != B[i]) {
                    flag = false;
                    break;
                } 
            }
        }
        else {
            flag = false;
        }
        
        if (flag) {
            System.out.println("Arrays are equal");
        }
        else {
            System.out.println("Arrays are not equal");
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

1 Like


Related Questions