Array string compare with a master string.

How can I read string from an array and compare with a master string ?
So far read a 80 lines of string and store in array is not the problem. My problem start when I try to compare my master sting with the array string. Basically I want to compare how many letters of every single string are iqual to the master string and display the result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main(){

	std::string master = "Brown Dog";
	char s2[] = "Brown Cow";
	int cnt = 0;

	for (unsigned int i = 0; i < master.length(); i++){
		if (master.at(i) == s2[i])
			cnt++;
	}

	std::cout << cnt << std::endl;
	
	return 0;
}


This works however it does have some logic issues that can arise quickly. Hope this points you in a helpful direction.
Last edited on

probably I don't explain myself very well. My problem basically is in the string compare, i'm looking to display how many letter I got correct from every string. this is what I want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this is an example the order of the letter "A & B" could be different
 string master string: "ABABBBBBABBAAA"
cout<< string a[0];
// output on display  "AAABBB BABBAAA";
cout << string a[1];
// output on display: "ABABBBBBABBBB";
cout<< string a[2];
// output on display: "ABABBBBBABBAAA";
 cout<<string a [3];
// output on display: "AABABABABBAA  BA";
 cout<<outputCompare<< "correct" << "string a[0]" << endl // 12 correct string a[0]
 cout<<outputCompare<< "correct"<<"string a[1]" << endl  // 11 correct string a[1]
cout<< outputCompare<< "correct"<<"string a[2]" << endl //  14 correct string a[2]
cout<< outputCompare<< "correct"<<string .........
Just turn mobotus's code into a function and call that function for each string.

like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 compare::mobotuscompareString(){

	std::string master = "ABABABABA";
	char s2[] = "Brown Cow";
	int cnt = 0;

	for (unsigned int i = 0; i < master.length(); i++){
		if (master.at(i) == s2[i])
			cnt++;
	}
//I don't get this part: how I call the function and the char s2[] get the next value
 for ( int I=0; I< array_string[]; I++)
{
mobotuscompareString.....
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void compare::mobotuscompareString(std::string compareThis){

	std::string master = "ABABABABA"; //or refer to master somewhere else, or pass master as argument
	int cnt = 0;

	for (unsigned int i = 0; i < master.length(); i++){ //there's a serious error waiting to happen here :P
		if (master.at(i) == compareThis.at(i))
			cnt++;
	}
	cout << cnt << "correct comparing" << compareThis << "to master " << master << endl;
	// you could also change the return type to int and just return cnt and have the main program do the output
}

// then in main,
int main () {
	// do stuff to get strings
	for (int i = 0; i < numberOfStringsRead; i++) {
		compare::mobutuscompareString(a[i]); // try to have a more descriptive name than "a"
	}

	// do other stuff
}
Last edited on
Topic archived. No new replies allowed.