如何显示字符串中的字符重复?

问题描述 投票:-2回答:6

我已经写了一个程序显示在字符串中的字符重复(一个或多个),但它再次显示字符,如果它的2倍以上。任何解决方案找到它到底是什么?

//查找字符串中的字符重复........

#include<iostream>
using namespace std;
int main()
{
    int i,j;
    char ar[100];
    cout<<"enter string:";
    cin.getline(ar,100);
    for(i=0;ar[i]!='\0';i++)
    {
        for(j=i+1;ar[j]!='\0';j++)
        {
            if(ar[i]==ar[j])
            {  
                cout<<ar[i]<<endl;
                break;
            }
        }
    }

    system("pause");
    return 0;
}
c++
6个回答
3
投票

另一种方法是将字符字符串中进行排序,然后检查排序字符串。

重复的字符会很容易找到,因为它们会彼此相邻。


2
投票

你应该跟踪每个字符出现了多少次:

int count[256]; // <-- assuming char is 8 bytes
for(i=0;i!=256;++i) 
{
    count[i] = 0; // <-- set all counts to be zero
}
for(i=0;ar[i]!='\0';i++)
{
    count[ar[i]] = count[ar[i]] + 1;

    // now you can check if count is 1, and if so then do whatever
}

1
投票

这里是示例代码找到字符串重复字符。完整的代码将可在http://java2novice.com/java-interview-programs/duplicate-string-character-count/

public void findDuplicateChars(String str){

    Map<Character, Integer> dupMap = new HashMap<Character, Integer>(); 
    char[] chrs = str.toCharArray();
    for(Character ch:chrs){
        if(dupMap.containsKey(ch)){
            dupMap.put(ch, dupMap.get(ch)+1);
        } else {
            dupMap.put(ch, 1);
        }
    }
    Set<Character> keys = dupMap.keySet();
    for(Character ch:keys){
        if(dupMap.get(ch) > 1){
            System.out.println(ch+"--->"+dupMap.get(ch));
        }
    }
}


1
投票

我希望这会帮助:

#include <iostream>
using namespace std;

int main() {
    char str[50];
    cout << "Enter a string" << endl;
    gets(str);

    for(int i=0; str[i]!='\0'; i++)
    {
        for(int j=i+1; str[j]!='\0'; j++)
        {
            if(str[i]==str[j])
            cout << "Character " << str[i] << " is repeated" << endl;
        }
    }


    return 0;
}

0
投票

下面是代码适用于[A-Z],但它是非常节省空间。因为它仅使用两个整数找到解决方案。谢谢。

public class AllDuplicatesInString
{
    static class BitSet
    {
        int justPresent, moreThanOnce;

        BitSet() 
        {
            justPresent = moreThanOnce = 0;
        }

        void set(int k)
        {
            if(isSetJustPresent(k))
            {
                k = k - 'a';
                moreThanOnce = moreThanOnce | (1<<k);
                return;
            }
            k = k - 'a';
            justPresent = justPresent | (1<<k);
        }
        boolean isSetJustPresent(int k)
        {
            k = k - 'a';
            return (justPresent & (1<<k))!=0;
        }
        boolean isSetMoreThanOnce(int k)
        {
            k = k - 'a';
            return (moreThanOnce & (1<<k))!=0;
        }
    }
    public static String duplicateChars(String str)
    {
        if(str==null || str.equals("")){
            throw new NullPointerException();
        }
        BitSet b = new BitSet();
        for(int i=0;i<str.length();i++){
            b.set(str.charAt(i));
        }
        StringBuilder stringBuilder = new StringBuilder();
        for(int i=0;i<26;i++){
            if(b.isSetMoreThanOnce(i+'a')){
                stringBuilder.append((char)(i+'a'));
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args)
    {
        String str = "aaaabbbbjjjjsfsfzcncnzcmcncmnczmjsdjs";
        System.out.println(duplicateChars(str));
    }
}

0
投票

下面是从GeeksForGeeks工作代码

// C program to count all duplicates from string using hashing 
# include <stdio.h> 
# include <stdlib.h> 
# define NO_OF_CHARS 256 

/* Fills count array with frequency of characters */
void fillCharCounts(char *str, int *count) 
{ 
   int i; 
   for (i = 0; *(str+i);  i++) 
      count[*(str+i)]++; 
} 

/* Print duplicates present in the passed string */
void printDups(char *str) 
{ 
  // Create an array of size 256 and fill count of every character in it 
  int *count = (int *)calloc(NO_OF_CHARS, sizeof(int)); 
  fillCharCounts(str, count); 

  // Print characters having count more than 0 
  int i; 
  for (i = 0; i < NO_OF_CHARS; i++) 
    if(count[i] > 1) 
        printf("%c,  count = %d \n", i,  count[i]); 

  free(count); 
} 

/* Driver program to test to pront printDups*/
int main() 
{ 
    char str[] = "test string"; 
    printDups(str); 
    getchar(); 
    return 0; 
} 
© www.soinside.com 2019 - 2024. All rights reserved.