Use modern C
gets()
has been dropped from the C standard library since C11 (1312 years) and was even then considered poor practice.
Consider alternatives like using fgets()
for all user input.
Test not needed
i < 512
is useless. For that to be false, prior gets()
would have overrun str[]
and that is undefined behavior (UB).
// for(i = 0; (i < 512 && str[i] != '\0'); i++)
for(i = 0; str[i]; i++)
Simplify
Code does either + scnt - scnt2
or - scnt + scnt2
. Consider forming the difference once.
int diff = scnt - scnt2;
...
str[i] += diff;
...
str[i] -= diff;
Use auto-formatting
Early null character
str[i] + scnt - scnt2
may result in 0 and then the later printf("\nEncrypted string: %s\n", str);
will not print the entire string.
Overflow
As str[i] + scnt - scnt2
may overflow or easily result in a value outside the char
range. Re-code to prevent overflow and nicely handle out-of-range results. Perhaps a limit on scnt, scnt2
like code did with x
?