Program to print the mismatched characters

Program to print the mismatched characters

Question

Write a program to print the mismatched characters from two array. Output must be like the one given below.

Logic

  • The logic is simple, compare each character of the same index.
  • If characters are mismatched then print both the characters.
  • If characters are same, then increment the index position of both arrays.
  • After comparing each index, if anyone array has more characters than another, print the remaining characters.

Program

#include <iostream>
using namespace std;
int main()
{
    char a[]={'a','b','c','d','e'};
    char b[] = { 'a','b','b','d','f','g'};
    int i=0,j=0;

    while(a[i]!='\0')
    {
        if(b[j]=='\0')          //IF REACHED LAST INDEX IN B ARRAY
            break;
        if(a[i]==b[j])
        {
            i++;                //INCREMENT INDEX OF BOTH ARRAY
            j++;
        } else
            cout<<a[i++]<<" "<< b[j++]<<" ";
    }
    while(a[i]!='\0')           //REMAINING ITEMS OF A
        cout<<a[i++]<<" ";
    while(b[j]!='\0')           //REMAINING ITEMS OF B
        cout<<b[j++]<<" ";
    return 0;
}</pre>

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x