Count number of different character between two strings of equal length in C++

Today, in this tutorial, we will get to know how to count the number of different characters between two strings of equal length in C++. The number of different characters in a string can be calculated by using two approaches. The first one is the brute force method and the other one is by counting the occurrences of number letters in the string. Below you can see the source code for counting the numbers of different characters between two strings of equal length in C++.

Method to count the number of different characters between two strings of equal length

  1. Brute force approach
    • First, compare the letters of the first string with the letters of the second string and find out the different characters which are in the first string and not in the second string.
    • Next, compare the letters of the second string with the letters of the first string, and find out the different characters that are, characters that are in the second string and not in the first string.
  2. By counting the frequency of the number of letters that appear
    • Firstly, declare two arrays for two strings of size 26 and initiate to 0.
    • Then traverse both the strings and find out number of occurrences of characters and increment them.
    • If the position of a letter in the first array has 0 occurrences and the second array has more than one occurrence then increment the count of different characters.

C++ program to count the number of different characters between two strings of equal length

We will see each of the methods one by one with example:

Brute force approach

Source code::

#include <iostream>
using namespace std;

int main()
{
  string str1,str2;
  int count=0;
  cout<<"Enter first string::";
  cin>>str1;
  cout<<"\nEnter second string::";
  cin>>str2;

  if(str1.length()==str2.length())
  {
    for(int i=0;i<str1.length();i++)
    {
      int flag=0;
      for(int j=0;j<str2.length();j++)
        {
          if(str1[i]==str2[j])
            flag=1;
        }
      if(flag==0)
        count++;
    }

    for(int i=0;i<str2.length();i++)
    {
      int flag=0;
      for(int j=0;j<str1.length();j++)
      {
        if(str2[i]==str1[j])
          flag=1;
      }
      if(flag==0)
        count++;
    }
  }
  else
  {
    cout<<"Strings have different lengths"<<endl;
    return -1;
  }
  cout<<"Number of different characters in strings ::"<<count<<endl;
  return 0;
}

 

Output::

Enter first string::asdf

Enter second string::dfgh
Number of different characters in strings ::4

By counting the frequency of characters

Source code::

#include <iostream>
using namespace std;

int main()
{
  string str1,str2;
  int count=0;
  cout<<"Enter first string::";
  cin>>str1;
  cout<<"\nEnter second string::";
  cin>>str2;
  int count1[26]={0},count2[26]={0};
  if(str1.length()==str2.length())
  {
    for (int i=0; i<str1.length(); i++)
      count1[str1[i] - 'a']++;

    for (int i=0; i<str2.length(); i++)
      count2[str2[i] - 'a']++;

    for (int i=0;i<26;i++)
    {
      if((count1[i]!=0 && count2[i]==0) || (count1[i]==0 && count2[i]!=0))
        count++;
    }
    cout<<"Number of different characters in strings ::"<<count<<endl;
    return 0;
  }
  else
  {
    cout<<"Strings have different lengths"<<endl;
    return -1;
  }
}

Output::

Enter first string::aacd

Enter second string::abcd
Number of different characters in strings ::1

Also see:: Remove duplicate characters from a string in c++

Print all unique words from a string in Java

Leave a Reply

Your email address will not be published. Required fields are marked *