统计一个字符在字符串中连续出现的次数

Count the number of times a character appears in a contiguous manner in a string

我是 Java 的新手。我正在尝试打印字符串中存在的字符及其计数。仅当旁边出现相同字符时计数才会递增。

例如:

I/O : Sssgs

O/P : S1s2g1s1

无论字符是否彼此相邻,计算每个字符的出现次数都会给出完整计数。篡改 i 和 j 循环会产生 OutOfBounds 错误。

      //ch[] is the String converted to a character array.
     //count[] is an array to store count of the characters      

    //Checks if present char and next char are same and increments count
    for(int i=0;i<ch.length;i++)    
    {
        count[i]=0;
        for(int j=0;j<ch.length;j++)
        {
            if(ch[i]==ch[j])
            {
                count[i]++;
            }
        }
    }

    //Prints Distinct char
    for(int i=0;i<ch.length;i++)
    {
        int j;
        for(j=0;j<i;j++)
        {
            if(ch[i]==ch[j])
            {
                break;
            }
        }

        if(i==j)
        {
            System.out.print(ch[i]+" "+count[i]);
        }
    }

输入 > HelloWorld

预期输出应该 > H1 e1 l2 o1 W1 o1 r1 l1 d1

我刚刚对您的代码进行了一些更正,下面是它的样子:

public static void main(String[] args) {
    String s = "Sssgs";
    char[] ch = s.toCharArray();
    int[] count = new int[20];

       for(int i=0;i<ch.length;i++)    
        {
            count[i]=0;
            for(int j=i;j<ch.length;j++)
            {
                if(ch[i]==ch[j])
                {
                    count[i]++;
                } else {
                    break;
                }
            }
        }

        //Prints Distinct char
        for(int i=0;i<ch.length;i += count[i])
        {
            System.out.print(ch[i] + "" +count[i]);
        }
}

当我只读取字符及其出现次数然后在迭代中跳转该数字时,大多数更改发生在 Prints Distincts 中。它让我停在下一个不同的字符

"Sssgs" 的输出是 "S1s2g1s1","HelloWorld" 的输出是 "H1e1l2o1W1o1r1l1d1"

我讨厌这个解决方案,但我猜你使用 char[] 是出于需要。如果不是强制性的,我建议您按照 Lino 的建议使用 StringBuilder

char blankChar = " ".charAt(0);
if (stringInput == null || "".equals(stringInput)) {
    System.out.println("Empty input");
}
char[] ch = stringInput.toCharArray();
char lastChar = ch[0];
int numAppearanceslastChar = 0;
for (char element : ch) {
    if (element == blankChar) {
        continue;
    }
    if (lastChar == element) {
        numAppearanceslastChar++;
    } else {
        System.out.print(lastChar+""+numAppearanceslastChar+" ");
        lastChar = element;
        numAppearanceslastChar = 1;
    }
}
System.out.println(lastChar+""+numAppearanceslastChar+" ");

输出:H1 e1 l2 o1 w1 o1 r1 l1 d1

解释:只读取整个单词一次(注意你将执行 3 次 for 循环)并将最后读取的字符与新读取的字符进行比较。如果它们匹配,则增加该字符的出现次数。如果不是,则打印它们并将新的字符设置为最后一次读取。 当你结束阅读这个词时,打印最后一个阅读的字符。

永远记得保持简单!并清理(如果收到 null 或空,您将在此代码中得到一个 nullPointer,只需将其写在那里以指出它)。

我想到了这个:

public static String count(String in) {
    if (in == null || in.isEmpty()) {
        return in;
    }
    int length = in.length();
    if (length == 1) {
        return in + '1';
    }
    StringBuilder out = new StringBuilder(length << 1);

    char previous = in.charAt(0);
    int count = 1;
    for (int i = 1; i < length; i++) {
        char current = in.charAt(i);
        if (previous == current) {
            count++;
        } else {
            out.append(previous).append(count);
            previous = current;
            count = 1;
        }
    }
    return out.append(previous).append(count).toString();
}

处理 null 和空字符串。和带有 length == 1 的字符串(就是 string + 1)。

此解决方案也不需要创建额外的 char[] 数组,因为它使用 charAt

这是一个简单的解决方案,它不使用任何额外的数组,而是在下一个不同的字符时直接打印计数的字符

char prevChar = ch[0];
int count = 1;
for (int i = 1; i < ch.length; i++) {
  if (ch[i] != prevChar) {
    System.out.printf("%c%d ", prevChar, count);
    count = 1;
    prevChar = ch[i];
  } else {
    count++;
  }
}
System.out.printf("%c%d ", prevChar, count);