mercredi 13 juillet 2022

cout printing a large number in cpp

#include <iostream>

using namespace std;

int countChars(string s1, string s2) {

  int a = 0, result = 0, i = 0;
  for(i =0; i<s1.size(); i++) {
    if(a == s2.size()) {
      break;
    }
    if(s1.at(i) == s2.at(a)) {
      a++;
    } else {
      result++;
    }
  }

  cout << (s2.size() - a - 1) << "\n";
  result += (s1.size()-i + s2.size()-a-1);

  return result;
}

int main() {
  cout << countChars("madame", "madam");
}

The above code is printing

18446744073709551615

0

I am not able to understand the reason behind the output of the problem. Can anyone explain why is cout << (s2.size() - a - 1) << "\n"; printing 18446744073709551615?

Aucun commentaire:

Enregistrer un commentaire