Problem isn't answer true.(2)

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++)
{

for(j=0;j<=6;j++)
{

if(s1[i]==s2[i]){counter++;}

}

}

cout<<"Ortak Eleman Sayisi : "<<counter++<<endl;
return 0;

}
if(s1[i]==s2[i]){counter++;}

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.
How is it going to be ?
What's the correct answer for these two strings?

"a472k6A"
"17A89k5"
I dont understand ,ı just want which ı wrote up the questin answers.(C++ code)
You need to refine this (up to you) but it shows (up to you to find) where you were going off track:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    const char* s1 = "a472k6A";
    const char* s2 = "17A89k5a2";
    
    int counter = 0;
    
    for(size_t i = 0; i < strlen(s1); i++)
    {
        for(size_t j = 0; j < strlen(s2); j++)
        {
            if( s1[i] == s2[j])
            {
                cout << "Match found: " << s1[i] << " = " << s2[j] << endl;
                counter++;
            }
        }
    }
    cout << "Ortak Eleman Sayisi : " << counter++ << endl;
    
    return 0;
}

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"

Topic archived. No new replies allowed.