<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java 程序:查找字符串中字符的出現 > 原文: [https://beginnersbook.com/2019/04/java-program-to-find-the-occurrence-of-a-character-in-a-string/](https://beginnersbook.com/2019/04/java-program-to-find-the-occurrence-of-a-character-in-a-string/) 在本教程中,我們將編寫一個 Java 程序來查找`String`中字符的出現。 ## 編程以查找字符串中字符的出現 在這個程序中,我們發現`String`中每個字符的出現。為此,我們首先創建一個大小為 256(ASCII 上限)的數組,這里的想法是將出現次數存儲在該字符的 ASCII 值中。例如,`'A'`的出現將存儲在計數器[65]中,因為`A`的 ASCII 值是 65,類似地,其他字符的出現存儲在它們的 ASCII 索引值中。 然后我們創建另一個數組`array`來保存給定`String`的字符,然后我們將它們與`String`中的字符進行比較,當找到匹配時,使用`counter`數組顯示該特定字符的計數。 ```java class JavaExample { static void countEachChar(String str) { //ASCII values ranges upto 256 int counter[] = new int[256]; //String length int len = str.length(); /* This array holds the occurrence of each char, For example * ASCII value of A is 65 so if A is found twice then * counter[65] would have the value 2, here 65 is the ASCII value * of A */ for (int i = 0; i < len; i++) counter[str.charAt(i)]++; // We are creating another array with the size of String char array[] = new char[str.length()]; for (int i = 0; i < len; i++) { array[i] = str.charAt(i); int flag = 0; for (int j = 0; j <= i; j++) { /* If a char is found in String then set the flag * so that we can print the occurrence */ if (str.charAt(i) == array[j]) flag++; } if (flag == 1) System.out.println("Occurrence of char " + str.charAt(i) + " in the String is:" + counter[str.charAt(i)]); } } public static void main(String[] args) { String str = "beginnersbook"; countEachChar(str); } } ``` **輸出:** ![Java program to find the occurrence of a character in a string](https://img.kancloud.cn/b2/2b/b22bba61b9efd9d2febd576a807a2724_676x400.jpg) #### 相關的 Java 程序 1. [Java 程序:按字母順序排序字符串](https://beginnersbook.com/2018/10/java-program-to-sort-strings-in-an-alphabetical-order/) 2. [Java 程序:檢查元音和輔音](https://beginnersbook.com/2017/09/java-program-to-check-vowel-and-consonant-using-switch-case/) 3. [Java 程序:查找字符串中的重復字符](https://beginnersbook.com/2014/07/java-program-to-find-duplicate-characters-in-a-string/) 4. [Java 程序:檢查回文串](https://beginnersbook.com/2014/01/java-program-to-check-palindrome-string-using-recursion/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看