-11
// Iterate(loop/repetition) over the word
for(int i = 0; i < (int)word.size(); i++ ){
    // Get a character
    char ch = word.at(i);

    // If the character matches the character we're looking for
    if(searchCh == ch){

        // Increment a counter
        counter++; // counter = counter + 1

What does word.at(i) mean in the operator or what does the "at" operator do in C++? For example, string.at or word.at

Giorgio
  • 25
  • 1
  • 8

2 Answers2

2

You probably mean "how is word.at(i) different from word[i]"?

word.at(i) generally checks whether i is in range and throws an exception if not. word[i] just is undefined behaviour if i is out of range.

Also, with word[word.size()] you can access the implicit trailing '\0'-byte, but for word.at(word.size()) the index is out of range.

ex-bart
  • 1,292
  • 7
  • 9
0

at() is a member function in C++. s.at() returns the letter at position i in the string. But if the position is not in the range, it throws an exception.

Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98