TechTorch

Location:HOME > Technology > content

Technology

Finding and Printing Duplicate Characters in a String Using C

January 17, 2025Technology2265
Introduction In the field of software engineering, effective string ma

Introduction

In the field of software engineering, effective string manipulation is essential for various applications, including data analysis, data processing, and more. One common operation is to identify and print the duplicate characters in a given string. This article explores several C programming methods to achieve this, ensuring that you can efficiently analyze and process strings in your applications.

Method 1: Using Arrays for Frequency Counting

This method involves counting the frequency of each character in the string and then printing those characters that appear more than once. Below is a C program implementing this approach:

#include stdio.h#include string.hint main() {    char string[10];    int c  0;    int count[26]  {0};    printf("Enter a string: ");    scanf("%s", string);    while (string[c] ! '0') {        if (string[c]  'a' || string[c]  'A') {            count[string[c] - 'a']  ;        } else if (string[c]  'z' || string[c]  'Z') {            count[string[c] - 'A']  ;        }        c  ;    }    for (c  0; c  26; c  ) {        if (count[c]  1) {            printf("%c %d
", c   'a', count[c]);        }    }    return 0;}

This program first prompts the user to enter a string and then counts the frequency of each letter using an array `count`. It prints out characters that appear more than once along with their count.

Method 2: Using an Array for Character Duplicates

This section presents another approach using a similar technique, but with a more straightforward implementation:

#include stdio.h#include string.hint main() {    char string[]  "TutorialsPoint";    int count;    for (int i  0; i  strlen(string); i  ) {        count  1;        for (int j  i   1; j  strlen(string); j  ) {            if (string[i]  string[j]  string[i] ! ' ') {                count  ;                // Set string[j] to 0 to avoid printing visited character                string[j]  0;            }        }        // A character is considered as duplicate if count is greater than 1        if (count  1  string[i] ! 0) {            printf("%c %d
", string[i], count);        }    }    return 0;}

This program iterates through the string to find duplicates, incrementing the count for each matching character and marking the duplicates once found. Finally, it prints the character and its frequency if the count is greater than 1.

Method 3: Utilizing a Class for Duplicate Character Counting

This method employs a class-based approach to count and print the duplicate characters in a string. Below is the C implementation:

#include iostreamusing namespace std;#define NO_OF_CHARS 256class duplicate_char {public:    void charCounter(char str[], int count[]) {        int i;        for (i  0; str[i] ! '0'; i  ) {            count[str[i]]  ;        }    }    void printDuplicateCharacters(char str[]) {        int count[NO_OF_CHARS]  {0};        charCounter(str, count);        for (int i  0; i  NO_OF_CHARS; i  ) {            if (count[i]  1) {                printf("%c %d
", i, count[i]);            }        }    }};int main() {    duplicate_char dupchar;    char str[]  "TutorialsPoint";    (str);    return 0;}

This class-based approach encapsulates the functionality of counting and printing duplicates. The `charCounter` method counts the frequency of each character, and the `printDuplicateCharacters` method prints the characters with counts more than 1.

Conclusion

Effective string manipulation in C is crucial for various applications, and identifying and printing duplicate characters is a common task. The methods presented here can be adapted and utilized in your code to handle string analysis efficiently. Whether you prefer a straightforward procedural approach or a more encapsulated class-based method, these techniques offer versatile solutions for your needs.