Skip to main content
edited body
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92

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?

Use modern C

gets() has been dropped from the C standard library since C11 (13 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?

Use modern C

gets() has been dropped from the C standard library since C11 (12 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?

added 2 characters in body
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92

Use modern C

gets() has been dropped from the C standard library since C11 (13 years) and was even then considerconsidered 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. Re-code to prevent overflow and nicely handle out-of-range results. Perhaps a limit on scnt, scnt2 lieklike code did with x?

Use modern C

gets() has been dropped from the C standard library since C11 (13 years) and was even then consider 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 liek code did with x?

Use modern C

gets() has been dropped from the C standard library since C11 (13 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?

added 200 characters in body
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92

Use modern C

gets() has been dropped from the C standard library since C11 (13 years) and was even then consider 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 liek code did with x?

Use modern C

gets() has been dropped from the C standard library since C11 (13 years) and was even then consider 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++)

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 liek code did with x?

Use modern C

gets() has been dropped from the C standard library since C11 (13 years) and was even then consider 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 liek code did with x?

Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92
Loading