CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2017
    Posts
    1

    Troubleshooting comparison of two string arrays

    I'm having some difficulty with the comparison of two string arrays and would appreciate any help to guide me in the right direction.

    The first array: String [] sentence1 = {"alpha", "bravo", "charlie", "delta"};
    Second array: String [] sentence2 = {"bravo", "alpha", "charlie", "charlie"};

    I'm trying to create a counter for any instances of strings that are present in both arrays, but NOT at the same position. For example, the "alpha" string and "bravo" string would make the counter go up by two, as they are present in both arrays, but not in the same position. I also want to have it so that the counter does NOT take into consideration a string like "charlie", which is present at both identical locations (sentence1[2] & sentence2[2]) AND at different locations (sentence1[2] & sentence2[3]).
    In this example, the counter should be equal to 2 (1 for the "alpha" strings and 1 for the "bravo" strings. The "charlie" string should not be counted, as even though it's in different positions in sentence1[2] and sentence2[3], the fact that it appears in the identical position sentence1[2] and sentence2[2] will not affect the counter).
    This is the code I have so far:

    Code:
    public class TestProject 
    
    {
    
        public static void main(String[] args)
    
        {
            String [] sentence1 = {"alpha", "bravo", "charlie", "delta"};
            String [] sentence2 = {"bravo", "alpha", "charlie", "charlie"};
            int wordCounter = 0;
    
            for (int i=0; i < sentence1.length; i++){
                if (!(sentence1[i].equals(sentence2[i])) && (sentence2[0].equals(sentence1[i]) || sentence2[1].equals(sentence1[i]) || sentence2[2].equals(sentence1[i]) || sentence2[3].equals(sentence1[i])))
                    wordCounter++;}
    
            System.out.print(wordCounter);
        }
    }
    While I do get the right answer for the example above, if I change the arrays to the following, I get the wrong answer (3 instead of 2):

    The first array: String [] sentence1 = {"charlie", "alpha", "charlie", "bravo"};
    Second array: String [] sentence2 = {"bravo", "alpha", "alpha", "charlie"};

    In this example, I expect the word counter to read 2 (1 for the "bravo" strings at sentence1[3] and sentence2[1] and 1 for the "charlie" strings at sentence1[2] and sentence2[3]. I DO NOT want the "charlie" string to be counted again at sentence1[0] and sentence2[3]. The "alpha" string is not counted since it is in the identical position at sentence1[1] and sentence2[1], even though it appears at different positions at sentence1[1] and sentence2[2].)

    Any suggestions would be greatly appreciated! Ideally I would like this code to work with any combination of the four strings (alpha, bravo, charlie, & delta) in either one of the two arrays.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,875

    Re: Troubleshooting comparison of two string arrays

    Look at using nested loops.
    The loops' indexes would allow you to detect if two equal Strings were in the same location
    Norm

  3. #3
    Join Date
    Feb 2017
    Posts
    639

    Re: Troubleshooting comparison of two string arrays

    Quote Originally Posted by ade555 View Post
    Any suggestions would be greatly appreciated! Ideally I would like this code to work with any combination of the four strings (alpha, bravo, charlie, & delta) in either one of the two arrays.
    If I get your logic right it is:

    1. Repeat 2 for each of the four strings (alpha, bravo, charlie and delta).
    2. If the string doesn't occur at the same position anywhere in the two string arrays do 3.
    3. Increment a counter with the fewest number of times the string occurs in any of the two string arrays.

    You'll need a couple of convenience functions.

    First there's samePos. It's called with two string arrays and a string. If the string occurs at the same position in the two arrays then SamePos returns true otherwise false.

    Then there's numOccur. It's called with a string array and a string. It returns an integer telling how many times the string occurs in the string array.

    Finally there's minNum. It's called with two integers and returns the smallest one.

    It was some time since I did any Java but based on your code it would look something like this,

    Code:
        String [] theStrings = {"alpha", "bravo", "charlie", "delta"};
    
        String [] sentence1 = {"alpha", "bravo", "charlie", "delta"};
        String [] sentence2 = {"bravo", "alpha", "charlie", "charlie"};
        int wordCounter = 0;
    
        for (int i=0; i < theStrings.length; i++) {
             if (!samePos(sentence1, sentence2, theStrings[i]) {
                wordCounter += minNum(numOccur(sentence1, theStrings[i])), numOccur(sentence2, theStrings[i])));
            }
        }
    Last edited by wolle; June 14th, 2017 at 12:11 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured