0

How to compare two character arrays ?

character arrays

10th Apr 2017, 3:40 AM
Zahid Ansari
Zahid Ansari - avatar
4 Réponses
+ 11
If you want to do it the manual way though: bool is_equal = true; if (size_of(array1) != size_of(array2)) { is_equal = false; } for (int i = 0; i < (size_of(array1)/size_of(array1[0])); i++) { if (array1[i] != array2[i]) { is_equal = false; } } if (is_equal) { std::cout << "Arrays are equal."; } else { std::cout << "Arrays are not equal."; }
10th Apr 2017, 4:16 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Use strcmp from the <cstring> header: // strcmp returns 0 if the strings are equal bool equal = strcmp(string1, string2) == 0;
10th Apr 2017, 3:50 AM
Squidy
Squidy - avatar
+ 5
swapping
10th Apr 2017, 4:32 AM
Saumya
Saumya - avatar
+ 3
string s = new string(a); string t = new string(b); int answer = string.Compare(s, t); if(answer ==0) { //Equal }
10th Apr 2017, 4:07 AM
Aditya Gupta
Aditya Gupta - avatar