Ваше switch
выполнялось выполнено , но тело каждого case
содержало слишком много закомментированных частей.
Используйте fgets
вместо scanf
для фразыпоскольку scanf
получит только первое слово.
Примечание: Я бы использовал #if 0
и #endif
вместо /*
и */
для комментирования блоков кода.
Обратите внимание, что, хотя вы можете смешивать scanf
и fgets
, я рекомендовал использовать только fgets
и strtol
.
Вот очищенный и рабочий код [прошу прощения за чистую стирку]:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char word[100];
void
get(void)
{
fgets(word,sizeof(word),stdin);
char *cp = strchr(word,'\n');
if (cp != NULL)
*cp = 0;
}
int
main()
{
char letter;
int i;
int j;
int x;
int shift;
int stringLength;
printf("Enter d to DECRYPT.\n");
printf("Enter e to ENCRYPT.\n");
get();
letter = word[0];
if (letter == 'd' || letter == 'D') // d = 0, e = 1
{
x = 0;
}
else if (letter == 'e' || letter == 'E') {
x = 1;
}
else {
printf("The letter you entered was neither d or e!\n");
return 0;
}
printf("\nEnter the number of shifts:\n");
get();
shift = strtol(word,NULL,10);
printf("\nEnter the word or phrase you would like to Encrypt or Decrpyt\n");
#if 0
scanf("%s", word);
#else
get();
#endif
printf("\nThe word you entered was:\n%s\n", word);
stringLength = strlen(word);
printf("\nThe size of the string is: %i\n", stringLength);
printf("x=%d\n", x);
switch (x) {
case 0: // decrypt
printf("decrypt case\n");
for (i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] - shift;
#if 0
while (i < stringLength) {
word[i] = word[i] - shift;
i++;
}
#endif
printf("The Decrypted word is: %s\n", word);
break;
case 1: // encrypt
printf("encrypt case\n");
for (i = 0; (i < 100 && word[i] != '\0'); i++)
word[i] = word[i] + shift;
#if 0
while (i < stringLength) {
word[i] = word[i] + shift;
i++;
}
#endif
printf("The Encrypted word is: %s\n", word);
break;
}
return 0;
}