我编写了一个程序来显示字符串中的重复字符,但如果字符超过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;
}
答案 0 :(得分:3)
另一种方法是对字符串中的字符进行排序,然后检查已排序的字符串。
重复的字符很容易找到,因为它们会彼此相邻。
答案 1 :(得分: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
}
答案 2 :(得分: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));
}
}
}
答案 3 :(得分: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;
}
答案 4 :(得分: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));
}
}
答案 5 :(得分:0)
// 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;
}
答案 6 :(得分:0)
如果您想维持订单,可以在下面尝试我的代码。我只是为了小写而已。
诀窍是保持每个字符的频率计数并在循环中每次检查它。
#include <iostream>
#include <cstring>
using namespace std;
char* print_duplicates(const char str[], int n);
int main(){
char * actual = new char(20);
char * dup = new char(20);
cout << "Enter the string: ";
cin >> actual;
int size = strlen(actual);
dup = print_duplicates(actual,size);
cout << "The duplicates are: " << dup << endl;
return 0;
}
char* print_duplicates(const char str[], int n){
int freq[26]{0}, index{0};
char* duplicates = new char(20);
for (int i=0;i<n;++i){
for (int j=i+1;j<n;j++){
if (freq[str[i]-'a'] == 0){
if(str[i]==str[j]){
++freq[str[i]-'a'];
duplicates[index++] = str[i];
}
}
}
}
return duplicates;
}