在 C++ 中打印输入字符串中的所有重复项
在此问题中,给定一个字符串,我们必须找到字符串中所有重复的字符及其在字符串中的出现次数。
我们举一个例子来理解这个问题:
Input: TutorialsPoint Output: t (3) o (2) i (2)
说明- 每个字符的出现频率为:t → 3; u → 1; o → 2; r → 1; i → 2; a → 1; s → 1; n → 1。
现在,为了解决这个问题,我们将找出字符数,并将其从字符串中存储在一个数组中。然后打印 freq 处字符和出现次数。它大于 1。
示例
# include <iostream>
using namespace std;
# define NO_OF_CHARS 256
class duplicate_char{
public :
void charCounter(char *str, int *count){
int i;
for (i = 0; *(str + i); i++)
count[*(str + i)]++;
}
void printDuplicateCharacters(char *str){
int *count = (int *)calloc(NO_OF_CHARS, sizeof(int));
charCounter(str, count);
int i;
for (i = 0; i < NO_OF_CHARS; i++)
if(count[i] > 1)
printf("%c\t\t %d \n", i, count[i]);
free(count);
}
};
int main(){
duplicate_char dupchar ;
char str[] = "tutorialspoint";
cout<<"The duplicate characters in the string\n";
cout<<"character\tcount\n";
dupchar.printDuplicateCharacters(str);
return 0;
}输出
字符串字符数中的重复字符
i 2 o 2 t 3
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP