Failing tests when including .in files
My code works fine when I manually input the shift and text, but when I
try to test it using the .in files (e.g. c1.in) it just prints out "Enter
shift: Enter text:" and then exits. I'm not sure how to fix my code so
that it can pass the tests. Any help would be appreciated.
My code:
#include <stdio.h>
int encode(int ch, int shift);
int rotate_right(int ch, int shift);
int rotate_left(int ch, int shift);
int main() {
int ch;
int shift;
printf("Enter shift: ");
scanf("%d", &shift);
printf("Enter text: ");
while((ch = getchar()) != EOF) {
putchar(encode(ch, shift));
}
return 0;
}
int encode(int ch, int shift) {
char newLetter;
shift = shift % 26;
if (shift > 0) {
newLetter = rotate_right(ch, shift);
} else if (shift < 0) {
newLetter = rotate_left(ch, shift);
} else {
newLetter = ch;
}
return newLetter;
}
int rotate_right(int ch, int shift) {
char newLetter;
if (ch > 64 && ch < 91) {
if (shift < 91 - ch) {
newLetter = ch + shift;
} else {
newLetter = ch + shift - 26;
}
} else if (ch > 96 && ch < 123) {
if (shift < 123 - ch) {
newLetter = ch + shift;
} else {
newLetter = ch + shift - 26;
}
} else {
newLetter = ch;
}
return newLetter;
}
int rotate_left(int ch, int shift) {
char newLetter;
shift = -shift;
if (ch > 64 && ch < 91) {
if (shift < ch - 64) {
newLetter = ch - shift;
} else {
newLetter = ch - shift + 26;
}
} else if (ch > 96 && ch < 123) {
if (shift < ch - 96) {
newLetter = ch - shift;
} else {
newLetter = ch - shift + 26;
}
} else {
newLetter = ch;
}
return newLetter;
}
c1.in:
19
Aolyl pz h apkl pu aol hmmhpyz vm tlu
Dopjo ahrlu ha aol msvvk, slhkz vu av mvyabul;
Vtpaalk, hss aol cvfhnl vm aolpy spml
Pz ivbuk pu zohssvdz huk pu tpzlyplz.
Thanks.
No comments:
Post a Comment