Duplicate character in a string in array c++. It is throwing garbage please help me out

Viewed 28

I have created a program for finding the duplicate character in a string. It is throwing garbage please help me to find the error and also find out its solution. I have created two arrays array a and b array a is a character array and array b is a integer array. array a is storing the characyeer that occurred in the string and array b for storing their frequency. please help me to find out the error

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
    string s;
    int i,j,k,l,m,n=0;
    cout<<"Enter a string:";
    cin>>s;
    while (s[i]!='\0')
    {
        j++;
        i++;
    }
    char *a=(char *)malloc(j*sizeof(char));
    int *b = (int *)malloc(j*sizeof(int));
    for (i=0;i<j;i++)
    {
        b[i]=0;
    }
    for (i=0;i<j;i++)
    {
        l=0;
        for (k=0;k<i;k++)
        {
            if (s[i]==a[k])
            {
                b[k]++;
            }
            else
            {
                l++;
            }
        }
        if (l+1==i)
        {
            a[i]=s[i];
            b[i]++;
            n++;
        }
    }
    i=0;
    while (a[i]!='\0')
    {
        m++;
        i++;
        cout<<a[i];
    }
    for (i=0;i<m;i++)
    {
        if (b[i]>1)
        {
            cout<<a[i]<<"occurs "<<b[i]<<" times";
        }
    }
    return 0;
}
1 Answers

Let's actually use C++:

#include <cctype>
#include <iostream>
#include <string>
#include <unordered_map>

int main() {
  std::string sample{"rat cat bat"};
  std::unordered_map<char, int> letterFrequencies;

  for (auto c : sample) {
    if (std::isalpha(c)) {
      ++letterFrequencies[c];
    }
  }

  for (auto entry : letterFrequencies) {
    if (entry.second > 1) {
      std::cout << "'" << entry.first << "' occurs " << entry.second
                << " times.\n";
    }
  }
}

Output:

❯ ./a.out 
't' occurs 3 times.
'a' occurs 3 times.

Analyzing your code is difficult, as it seems to do far more than what's necessary. What it's doing unnecessarily is difficult to determine as your variable names are awful. Even if I wanted to use your two array method, I wouldn't. This can be done manually with a single array.

  • I don't know what the point of the nested loops are at all. All you need to do is count, and then analyze. That doesn't require nesting. It's two distinct loops.
  • You sized a wrong. It should be the size of the alphabet, not the size of your string that might have repeated characters that you are trying to count. But like I said, this array is unnecessary.

Shortcomings of the example above are that upper case and lower case letters are treated as different letters. It's an easy enough fix using another function from <cctype>.

It also explicitly only lists letters that were repeated.

std::unordered_map is a key-value data structure (similar to a python dictionary), usually implemented as a hash-map, meaning that lookups are very fast. Our keys are the letters of the string, the value is the count. The other key property is that when using operator[], if the key doesn't exist, it will be created for us.

Here is a different program that doesn't use a map.

#include <array>
#include <cctype>
#include <iostream>
#include <string>

int main() {
  std::string sample{"rat cat bat"};
  std::array<int, 26> letterFrequencies{0};

  for (auto c : sample) {
    if (std::isalpha(c)) {
      ++letterFrequencies[std::toupper(c) - 'A'];
    }
  }

  for (std::size_t entry = 0; entry < letterFrequencies.size(); ++entry) {
    if (letterFrequencies[entry] > 1) {
      std::cout << "'" << static_cast<char>(entry + 'A') << "' occurs "
                << letterFrequencies[entry] << " times.\n";
    }
  }
}

Output:

❯ ./a.out
'A' occurs 3 times.
'T' occurs 3 times.

This program treats upper case and lower letters as the same.

Related