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

[code lang=”c”]
#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>
[/code]

Sree Hari Sanjeev

The founder of Wisdom Overflow. Software Developer at Zoho Corporation.

Leave a Reply

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