Why is the wrong number of characters in "Edit2"?

enter image description here

enter image description here

if(!error) { char s_russian[] = { "àáâãä叿çèéêëìíîïðñòóôõöøùúûüýþÿ" }; char s_latin[] = { "abcdefghijklmnopqrstuvwxyz" }; char S_RUSSIAN[] = { "ÀÁÂÃÄŨÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞß" }; char S_LATIN[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; char s_num[] = { "0123456789" }; char s_esc[] = { "`~@#¹$;%^:&?*()-_=+|[{]}<>" }; char password_chars[] = ""; if(CheckBox1->Checked == true && CheckBox3->Checked == true) { strcat(password_chars, s_russian); } if(CheckBox2->Checked == true && CheckBox3->Checked == true) { strcat(password_chars, s_latin); } if(CheckBox1->Checked == true && CheckBox4->Checked == true) { strcat(password_chars, S_RUSSIAN); } if(CheckBox2->Checked == true && CheckBox4->Checked == true) { strcat(password_chars, S_LATIN); } if(CheckBox5->Checked == true) { strcat(password_chars, s_num); } if(CheckBox6->Checked == true) { strcat(password_chars, s_esc); } int password_chars_length = strlen(password_chars); Edit2->Text = password_chars_length; } 
  • Do you have Russian letters in what encoding? In UTF8, for example, Russian letters occupy 2 bytes. A 'Y' can be either 2 or 4 bytes - Mike
  • @Mike And how can I then calculate the length of the string without steaming with bytes? - aaa
  • And "not steaming" will not work. But then you probably still want to get some kind of symols from this line for the password and you will have to take into account their length, first to take 2 bytes when necessary and secondly not to get accidentally in the middle of a character. So it would be easier to use some strings from C ++, rather than the char type - Mike
  • @Mike, the fact is that if I select one parameter, digits, symbols, everything counts correctly, but if several then it is no longer true. - aaa
  • @Mike For example, Russian, the alphabet in upper and lower case considers true. With Latin as well. Latin + Russian in lower case displays correctly, so if you add to this in upper case is no longer true. - aaa

1 answer 1

 char password_chars[] = ""; /* ... */ strcat(password_chars, S_RUSSIAN); 

How does IT work for you? The program should fall with a terrible crash on the very first strcat() .

Or allocate under password_chars[] once as much space as it takes all character sets:

 char password_chars[sizeof(s_russian)+sizeof(s_latin)...]; 

Alternatively, use std::string , as you were originally suggested.

  • C strcat everything is fine, and displays all characters without problems. I need to know the selected character size as I create a random for password selection. - aaa
  • C strcat everything is fine - no, not normal. The fact that it works for you is just an accident. - PinkTux
  • Well, the main thing works :) - aaa
  • @ Yevgeny You have undefined behavior in the program. you copy a string to an unreserved memory area. 20 times everything will be ok, and 21 times it will do something unexpected, for example a screw will format - Mike
  • @Mike Yeah, remove the internet. - aaa