Duplicated Letters

I’ve been working on a program that counts unique words in a text file. It’s an interesting exercise and explores the larger realm of pattern matching. You can try such scanning on a smaller scale by coding a program that counts the frequency of letters within a string. And that task is presented as this month’s Exercise.

Start by prompting the user for string input. Have your code scan the string to determine whether any letters — A to Z, case-insensitive — appear more than once. If so, the duplicated letters are displayed. When no letters repeat, the string is flagged as having all unique letters.

Here’s a sample run:

Enter text: Unique words
Letter u repeats

And one to pass the test:

Enter text: Ugly Ed is an ox
String is unique: No two letters repeat.

You need not get fancy with the way the repeated letters are flagged. For example, my solution just coughs up a letter the first time it’s duplicated:

Enter text: My solution is nothing fancy.
Letter o repeats
Letter i repeats
Letter s repeats
Letter n repeats
Letter o repeats
Letter t repeats
Letter i repeats
Letter n repeats
Letter n repeats
Letter y repeats

If you like, you can write code that counts the duplicated letters and displays a tally, but this solution is optional. Also, it’s not necessary to distinguish between uppercase and lowercase letters; the code should be case-insensitive.

Please try this Exercise on your own before you check out my solution.

2 thoughts on “Duplicated Letters

  1. I threw this together quickly. It uses an int array indexed 0 to 25 to hold the counts of the letters A-Z, subtracting 65 from the ASCII value of each letter in the input string to index the array, and then adding 65 to the array indexes to get back to the letters.

    It doesn’t do exactly what you specified as it outputs all counts even if they are 0 or 1. (Since when did programmers ever write software that implemented the specification?) However, it does print a * if the count is 2 or more.

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

    void count_letters(char* sentence);

    int main()
    {
        char* sentences[] = {“Unique words”,
                             “Ugly Ed is an ox”,
                             “My solution is nothing fancy”};

        for(int s = 0; s < 3; s++)
        {
            count_letters(sentences[s]);
        }

        return EXIT_SUCCESS;
    }

    void count_letters(char* sentence)
    {
        int charindex;

        int lettercounts[26];
        for(int i = 0; i <= 25; lettercounts[i] = 0, i++);

        for(int c = 0, l = strlen(sentence); c < l; c++)
        {
            if(isalpha(sentence[c]))
            {
                charindex = toupper(sentence[c]) – 65;

                lettercounts[charindex]++;
            }
        }

        for(int i = 0; i <= 25; i++)
        {
            printf(“%c %d %c\n”, (i + 65), lettercounts[i], lettercounts[i] > 1 ? ‘*’ : ‘ ‘);
        }

        puts(“”);
    }

Leave a Reply