Assuming this code:
#include <iostream>
using namespace std;
int letters_counted_in_text( std::string const&text ) {
int count = 0;
string abc = "abcdefghijklmnñopqrstuvwxyzáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ";
for( unsigned i=0; i<text.length(); ++i )
for( unsigned j=0; j<abc.length(); ++j )
if( text.at( i )==abc.at( j ) )
{
count++;
j=abc.length();
}
return count;
}
int main() {
// your code goes here
string test = "Hola, cómo estás";
cout << letters_counted_in_text(test);
return 0;
}
why it has different behavior in codechef
:
OutPut:
13
But in ideone
is:
OutPut:
15
in cpp.sh
OutPut: is 15
To what can this behavior be? I'm sorry for my bad english I hope you can understand what I say?
It looks like you have a character encoding problem. In your source code several characters used are not members of ASCII. This leave you open to different encodings and different interpretations of extended ASCII.
For example, saving your source as UTF-8 and then opening with an editor that only reads raw ASCII, the strings come out
string abc = "abcdefghijklmnñopqrstuvwxyzáéÃóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÃÉÃÓÚ";
and
string test = "Hola, cómo estás";
That puts 15 characters in test
that are also in abc
because some of the characters took up more than one byte. Using std::wstring
instead of std::string
should help with this, but you also need to use widechar string literals
wstring abc = L"abcdefghijklmnñopqrstuvwxyzáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ";
and
wstring test = L"Hola, cómo estás";
and of course
int letters_counted_in_text(std::wstring const&text)
because we need to pass wstring
in to the function.
Here it is on ideone: http://ideone.com/fAVPKt
Now we're left with the question, "Why did this work on CodeChef?"