Skip to main content
Title, again.
Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418

Count Counting the number of occurrences of chars in one string, in another string

deleted 58 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Objective of posting this question is to get ideas of experts if there is any better way of write the belowThis program?

Program is to count counts the total number of times any character from the source string can be found in the target string.

E.g Source String - "Hello World". 
Target String - "llo" 
Output = 5 (as there are 3 l's and 2 o's)

Source String - "Hello World" 
Target String - "ooood" 
Output = 3 (as there are 2 o's and 1 d)

Is there any better way of write the below program?

public class CountOccurence {
public static int countChars(String target, String source){
    char a[] = source.toCharArray();

    int loc = 0, count = 0;
    boolean charRepeat = false;

    for (loc = 0; loc < source.length(); loc++) {
        charRepeat = false;
        for (int z = 0; z < loc; z++) {
            if (a[loc] == a[z]) {
                charRepeat = true;
                break;
            }
        }

        if (charRepeat == false) {
            int i = 0;
            while ((i = target.indexOf(a[loc], i)) != -1) {
                count++;
                i++;
            }
        }
    }
    return count;
}

public static void main(String[] args) {
    String target = "Hello World";
    String source = "oood";
    
    int count = countChars(target, source);
    System.out.println(count);
    
    source = "lld";
    count = countChars(target, source);
    System.out.println(count);
    
}

}

Objective of posting this question is to get ideas of experts if there is any better way of write the below program?

Program is to count the total number of times any character from the source string can be found in the target string.

E.g Source String - "Hello World". Target String - "llo" Output = 5 (as there are 3 l's and 2 o's)

Source String - "Hello World" Target String - "ooood" Output = 3 (as there are 2 o's and 1 d)

public class CountOccurence {
public static int countChars(String target, String source){
    char a[] = source.toCharArray();

    int loc = 0, count = 0;
    boolean charRepeat = false;

    for (loc = 0; loc < source.length(); loc++) {
        charRepeat = false;
        for (int z = 0; z < loc; z++) {
            if (a[loc] == a[z]) {
                charRepeat = true;
                break;
            }
        }

        if (charRepeat == false) {
            int i = 0;
            while ((i = target.indexOf(a[loc], i)) != -1) {
                count++;
                i++;
            }
        }
    }
    return count;
}

public static void main(String[] args) {
    String target = "Hello World";
    String source = "oood";
    
    int count = countChars(target, source);
    System.out.println(count);
    
    source = "lld";
    count = countChars(target, source);
    System.out.println(count);
    
}

}

This program counts the total number of times any character from the source string can be found in the target string.

E.g Source String - "Hello World" 
Target String - "llo" 
Output = 5 (as there are 3 l's and 2 o's)

Source String - "Hello World" 
Target String - "ooood" 
Output = 3 (as there are 2 o's and 1 d)

Is there any better way of write the below program?

public class CountOccurence {
public static int countChars(String target, String source){
    char a[] = source.toCharArray();

    int loc = 0, count = 0;
    boolean charRepeat = false;

    for (loc = 0; loc < source.length(); loc++) {
        charRepeat = false;
        for (int z = 0; z < loc; z++) {
            if (a[loc] == a[z]) {
                charRepeat = true;
                break;
            }
        }

        if (charRepeat == false) {
            int i = 0;
            while ((i = target.indexOf(a[loc], i)) != -1) {
                count++;
                i++;
            }
        }
    }
    return count;
}

public static void main(String[] args) {
    String target = "Hello World";
    String source = "oood";
    
    int count = countChars(target, source);
    System.out.println(count);
    
    source = "lld";
    count = countChars(target, source);
    System.out.println(count);
    
}

}
Post Migrated Here from stackoverflow.com (revisions)
Source Link
Sandeep
  • 193
  • 1
  • 1
  • 6

Count number of occurrences in string

Objective of posting this question is to get ideas of experts if there is any better way of write the below program?

Program is to count the total number of times any character from the source string can be found in the target string.

E.g Source String - "Hello World". Target String - "llo" Output = 5 (as there are 3 l's and 2 o's)

Source String - "Hello World" Target String - "ooood" Output = 3 (as there are 2 o's and 1 d)

public class CountOccurence {
public static int countChars(String target, String source){
    char a[] = source.toCharArray();

    int loc = 0, count = 0;
    boolean charRepeat = false;

    for (loc = 0; loc < source.length(); loc++) {
        charRepeat = false;
        for (int z = 0; z < loc; z++) {
            if (a[loc] == a[z]) {
                charRepeat = true;
                break;
            }
        }

        if (charRepeat == false) {
            int i = 0;
            while ((i = target.indexOf(a[loc], i)) != -1) {
                count++;
                i++;
            }
        }
    }
    return count;
}

public static void main(String[] args) {
    String target = "Hello World";
    String source = "oood";
    
    int count = countChars(target, source);
    System.out.println(count);
    
    source = "lld";
    count = countChars(target, source);
    System.out.println(count);
    
}

}