Question and code which ı wrote below.I wrote this code by using flow chart.Everything seems true but when ı compile tis code its answer wrong.Please help me.
Quesiton:
s1="a472k6A"
s2="17A89k5"
Two string are given.Write a C++ code to find the numbers of common characters.
Answer:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char* s1="a472k6A";
char* s2="17A89k5";
int counter=0;
int i,j;
for(i=0;i<=6;i++)
{
Your code only counts if s1[i]==s2[i]. Let's look at all those cases:
i = 0:
s1[i] = a
s2[i] = 1
NO MATCH.
i = 1:
s1[i] = 4
s2[i] = 7
NO MATCH.
i = 2:
s1[i] = 7
s2[i] = a
NO MATCH.
i = 3:
s1[i] = 2
s2[i] = 8
NO MATCH.
i = 4:
s1[i] = k
s2[i] = 9
NO MATCH.
i = 5:
s1[i] = 6
s2[i] = k
NO MATCH.
i = 6:
s1[i] = a
s2[i] = 5
NO MATCH.
No matches at all, so count will be zero. Your code is just doing entirely the wrong thing. You're meant to be looking for common characters, but your code is counting the number of times each word has the exact same character in the exact same place.
Your question is ambiguous, @whitesoldier.
Besides the fact that you are only comparing like positions in the array (and hoping that they have identical lengths) you haven't stated what you would like to happen if there are repeats in either string.
What answer would expect to get from
s1="1111abcdef"
s2="234511"
and what would you get if you put them in opposite order:
s1="234511"
s2="1111abcdef"