【问题标题】:Finding a repeated character in a string在字符串中查找重复的字符
【发布时间】:2013-08-17 13:25:12
【问题描述】:

问题说明如下:给定一个字符串和用户给出的一个字符,找出该字符(由用户给出)在字符串(也由用户给出)中重复的次数。

我有这段代码

public int repeticion (int s){
        int return = 0;
        int cont = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Write a string: ");
        String chain = in.next();
        System.out.println("Write the character: ");
        String character = in.next();
        if (chain.contains(character)) {
            cont = cont + 1;
        }
        System.out.println("The character repeats itself "+cont+"times");
        return return;

但正如您所见,.contains 只计算字符一次,而不是它在字符串中出现的次数。

【问题讨论】:

  • contains 只是检查 if 字符出现在字符串中,而不是 出现了多少次。您需要遍历每个字符并将其与用户字符进行比较。尝试使用yourString.charAt(index) 获取指定索引处的字符。请记住,索引的范围可以从 0 到 yourString.length() - 1。

标签: java string character repeat


【解决方案1】:

.contains() 只表示一个字符存在于一个字符串中,而不是多少。您需要遍历字符串的每个字符以检查它是否等于您正在搜索的字符。

String chain = "your string";
int cont = 0;
for(int i=0; i<chain.length(); i++) {
   if(chain.charAt(i) == character) {
      cont++;
   } 
}

【讨论】:

    【解决方案2】:

    您还可以反复检查字符是否在字符串中,获取该索引,然后检查从该索引之后到字符串末尾的子字符串。我推荐以下:

    int index = 0;
    int count = 0;
    while (chain.indexof(character, index) != -1  && index < chain.length()-1) {
        index = chain.indexof(character, index) + 1;
        count++;
    }
    

    【讨论】:

      【解决方案3】:

      Contains 只会告诉您角色是否存在。要实际计算该字符出现的次数,您需要遍历字符串并计算所选字符出现的次数。此方法可行:

      public countACharacter(char thecharacter, String stringtocountcharactersin) {
          int count = 0;
          for(int i = 0; i < stringtocountcharactersin.length(); i++) {
              if(stringtocountcharactersin.charAt(i) == thecharacter) {
                  count++;
              }
          }
          return count;
      }
      

      【讨论】:

        【解决方案4】:

        //没有字符串方法。 公共静态 void main(String[] args) {

            String ss="rajesh kumar";
        
            Field value = null;
            try {
                value = String.class.getDeclaredField("value");
            } catch (NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
            // It's private so we need to explicitly make it accessible.
        
            value.setAccessible(true);
        
            try {
                char[] values = (char[])value.get(ss);
        
                //String is converted to char array with out string functions
        
                for (int i = 0; i < values.length; i++) {
        
                    int count=0;
                    for(int j=0;j<values.length;j++)
                    {
                        if(values[i]== values[j])
                        {
                            count++;
                        }
                    }
                    System.out.println("\n Count of :"+values[i] +"="+count);
        
                }
        
                System.out.println("Values "+values[1]);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
        }
        

        }

        【讨论】:

          【解决方案5】:
          import java.io.*;
          
          public class Duplicate {
          
              public static void main(String[] args) throws IOException {
                  int distinct = 0;
                  int i = 0, j = 0;
                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                  System.out.println("Enter STRING : -");
                  String s = br.readLine();
                  try {
                      for (i = 0; i < s.length(); i++) {
                          while (s != null) {
                              s = s.trim();
                              for (j = 0; j < s.length(); j++) {
                                  if (s.charAt(i) == s.charAt(j)) {
                                      distinct++;
                                  }
                              }
                              System.out.println(s.charAt(i) + "--" + distinct);
                              String d = String.valueOf(s.charAt(i));
                              s = s.replaceAll(d, " ");
                              distinct = 0;
                          }
                      }
                  } catch (Exception e) {}
              }
          }
          

          【讨论】:

          • 您能否添加说明,说明您认为这可以回答问题的原因?没有它就很难看出你在做什么。
          【解决方案6】:
          public static void chars( String a, char k)
                 {
                     String z=""+k;
                     int s=0;
                     for(int i=0;i<a.length();i++)
          
                     {
          
                         if(z.equals(a.substring(i,i+1)))
                             s++;
          
                                                      }
          
                     System.out.println(s);
                 }
          

          您可以通过这种方式实现您的目标,您无需任何理由就使用整数 s,而您做错的是您使用 .contains() 方法而不是您应该将给定的字符与每个字符进行比较字符串并使用计数器来维护字符的次数,或者您可以通过将字符与空字符串(“”)连接来将字符转换为字符串,然后使用 .equals 方法,就像我在我的代码。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-05-09
            • 2020-02-24
            • 1970-01-01
            • 2018-08-27
            • 2014-06-01
            • 2019-11-17
            • 1970-01-01
            相关资源
            最近更新 更多