0

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

https://www.codechef.com/ide

But in ideone is:

OutPut:

15

https://ideone.com/jzbxA1

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?

Mani Deep
  • 1,298
  • 2
  • 17
  • 33
Angel Angel
  • 19,670
  • 29
  • 79
  • 105
  • Several of the characters you use are not represented in ASCII so different extensions of ASCII (or other encodings) could produce different results. Place a breakpoint on count++ and see what triggers it. – user4581301 Mar 18 '17 at 03:42
  • 1
    Come to think of it, here's what happens if I dump your strings to UTF-8 and then read it back as ASCII: "Hola, cómo estás" 15 characters. – user4581301 Mar 18 '17 at 03:50
  • @user4581301 It makes sense. But because codechef it is not the same, can it be by some compiler configuration? thanks for your time – Angel Angel Mar 18 '17 at 04:03
  • I think you need to `string test = u8"Hola, cómo estás";`, but I unfortunately don't know how to turn a UTF-8 string literal into a `wstring` to do the compare without converting to a stream and back. – user4581301 Mar 18 '17 at 04:07
  • I was wrong. Easier way to do it. – user4581301 Mar 18 '17 at 04:09

1 Answers1

1

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?"

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you for your time, I upvote and I will mark as accepted, and if there is another one that solves the CodeChef mystery I will remove it. – Angel Angel Mar 18 '17 at 04:21
  • I test your code in codechef and I get this -> `error: converting to execution character set: Invalid or incomplete multibyte or wide character` Maybe it helps to know why. But no error in ideone. – Angel Angel Mar 18 '17 at 04:29
  • 1
    @AngelAngel Got that myself. They are doing something weird with UTF-8. I don't know what, but I don't think it's kosher. – user4581301 Mar 18 '17 at 05:03