Java implements statistics on the number of times a string appears in another string.

Source: Internet
Author: User

Java implements statistics on the number of times a string appears in another string.

Regular expressions are not allowed to be used during interviews. Fortunately, this algorithm is still simple, but writing on the draft paper will inevitably lead to running exceptions. Well, the interviewer won, so we can actually code it out.

The following is the implementation code:

/*** Count the number of times a String appears in another String ***/public class CountHit {public static void main (String [] args) {String a = "123456abcde6ab "; string B = "6abc"; System. out. println (new CountHit (). hit (a, B ));} /***** @ param a * matched long String * @ param B * matched short String * @ return matching times */public int hit (String a, String B) {if (. length () <B. length () {return 0;} char [] a_t =. toCharArray (); char [] B _t = B. toCharArray (); int count = 0, temp = 0, j = 0; for (int I = 0; I <a_t.length; I ++) {// ensure that a continuous string B matches a specific segment of a if (a_t [I] = B _t [j] & j <B _t.length) {temp ++; j ++; // at this time, the continuous string B matches a certain segment of a if (temp = B _t.length) {count ++; temp = 0; j = 0 ;}/// if one character does not match, the temp count always else {temp = 0; j = 0 ;}} return count ;}}

:)


Java programming implements a method. The parameter is used to specify a string and count and output the number of times each character appears in the string (using the array method)

Sorry, I didn't see the question clearly. I used arrays and modified the Code as follows. The original code is also retained for reference:
Private String [] chars; // record character private int [] count; // total number of records public void countChar (String str) {// initialized array: here the array length, use the string length, but it may not be used, because the string may be repeated // do not want to use dynamic arrays, it is more troublesome, first demonstration // it is best to use List, chars = new String [str. length ()]; count = new int [str. length ()]; for (int I = 0; I <str. length (); I ++) {// determines whether the character exists for (int idx = 0; idx <chars. length; idx ++) {if (chars [idx] = null) {chars [idx] = str. substring (I, I + 1); // capture Count [idx] = count [idx] + 1; break;} if (chars [idx]. equals (str. substring (I, I + 1) {count [idx] = count [idx] + 1; break ;}}// output result for (int I = 0; I <chars. length; I ++) {if (chars [I]! = Null) System. out. println ("character [" + chars [I] + "] times:" + count [I]) ;}} public static void main (String [] args) {// test method: LetterCount test = new LetterCount (); test. countChar ("Adfasadfadaere, you have a good time ");}}
The original code is as follows. For more information, see:
Import java. nio. charset. charset; import java. util. hashMap; import java. util. map; import java. util. set; public class LetterCount {// used for statistical results. Use Character or char as the Key (Key) private Map <Character, Integer> countMap = new HashMap <Character, Integer> (); public void countChar (String str) {char [] chars = str. toCharArray (); // convert the string into a char array // loop, start to count for (char ch: chars) {// determine whether the character exists if (! CountMap. containsKey (ch) {// ...... the remaining full text>

Program to count the number of times a string appears in another string

/* TC2 compiled through */
/* The probability that the sub-string appears at each position of the Main string is equal. The sub-string matches from left to right */
/* Is the basic pattern matching algorithm. for improved algorithms, refer to Network */
/* If the substring is more likely to appear in the front of the primary string or in other cases, make corresponding improvements */
# Include <stdio. h>
# Include <conio. h>
# Include <string. h>
# Define arraysize 64
Int cont_str_contain_str (char * str, char * substr)
{
Int I;
Int Len;
Char * strend;
Len = strlen (substr );
Strend = str + strlen (str)-1;
For (I = 0; str <strend; I ++)
{
Str = strstr (str, substr );
If (! Str) break;
Str + = Len;
}
Return I;
}

Main ()
{
Int count;
Char s1 [arraysize], s2 [arraysize];
Clrscr ();
Printf ("string;"); scanf ("% s", s1 );
Printf ("substring;"); scanf ("% s", s2 );
Count = cont_str_contain_str (s1, s2 );
Printf ("\ n % d", count );
Getch ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.