How to find all repeating characters in a string with C++

1 Answer

0 votes
#include <iostream>
 
#define SIZE 256
  
void CountOccurrences(std::string str, char occurrences[]) {
    int i = 0;
    int size = str.length();
     
    while (i < size) {
        occurrences[(int)str[i]]++;
        i++;
    }
}
 
char PrintAllRepeatingCharacters(std::string str) {
    char occurrences[SIZE] = { 0 };
  
    CountOccurrences(str, occurrences);
  
    int i = 0;
    int size = str.length();
     
    while (i < size) {
        if (occurrences[(int)str[i]] > 1) {
            std::cout << str[i] << "\n";
            occurrences[(int)str[i]] = -1;
        }
        i++;
    }
     
    return -1;
}
  
int main() {
    std::string str = "c c++ csharp java php python";
     
    PrintAllRepeatingCharacters(str);
}
  
  
  
  
/*
run:
  
c
 
+
h
a
p
  
*/

 

 



answered Sep 4, 2022 by avibootz
edited Sep 4, 2022 by avibootz

Related questions

1 answer 116 views
1 answer 142 views
1 answer 119 views
1 answer 140 views
1 answer 112 views
...