Duplicate Character Counter in C

Published on 03 October 2022 (Updated: 03 October 2022)

Welcome to the Duplicate Character Counter in C page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

// Function to handle errors
int handle_error()
{
    printf("Usage: please provide a string\n");
    exit(0);
}

int main(int argc, char *argv[])
{
    /*
        Condition to check for No String as Input
    */
    if (argc !=2 )
    {
        handle_error();
    }
    /*
        Condition to check for No String as Input
    */
    if(strlen(argv[1]) == 0){
        handle_error();
    }
    int counter[256]={0};
    int len = strlen(argv[1]);
    for(int i=0;i<len;i++){
        counter[argv[1][i]-1]++;
    }
    int flag = 1;
    for(int i=0;i<len;i++){
        char c = argv[1][i];
        if(counter[c-1]>1){
            flag = 0;
            printf("%c: %d\n",c,counter[c-1]);
            counter[c-1]=0;
        }
    }
    if(flag == 1){
        printf("No duplicate characters\n");
    }
    return 0;
}

Duplicate Character Counter in C was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.