Issue generating ASCII characters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Issue generating ASCII characters

I have an issue with a code that generates ASCII characters to create a password. The length of said password is defined by the user. Sometimes (especially with length values such as 7, 8, 9 and 10), the code generates more than the requested characters until it reaches a length of 11 characters. Often, among these extra characters, the ASCII character 18 () is found. Does anyone know how I can treat this problem? Code: https://code.sololearn.com/cnjCaDP4qnHl/?ref=app

17th Jan 2019, 9:45 AM
zhoolf
zhoolf - avatar
2 Answers
+ 3
The last character of a string has to be a null character ('\0'). This indicates the end of the string. If there isn't one, printf just keeps trying to print what is in memory until it does find a null byte. char passwrd[len+1]; passwrd[len] = '\0'; Also, this code wouldn't work outside of Sololearn. To set the length of your array at runtime, you would normally have to use a dynamic array, with memory assigned via malloc().
17th Jan 2019, 10:21 AM
Zen
Zen - avatar
0
Thank you so much @Zen. I have made the necessary changes to my code and it works perfectly.
17th Jan 2019, 10:56 AM
zhoolf
zhoolf - avatar