Как удалить пробелы из окончательного выходного зашифрованного текста? - PullRequest
0 голосов
/ 20 января 2019

Как убрать пробелы между зашифрованным текстом, чтобы он печатался в виде одной строки чисел?Пример вывода: ## ## ## ## ## ##> ############

#include <stdio.h>

int main (void)
{
  char str[6];
  int i=0;
  int key;

  printf("Enter 6 letter password (all caps): ");
  scanf("%s",str);

  printf ("Enter a single digit cipher key (between 2-8):");
  scanf ("%d", &key);
  printf("The ciphertext is: ");
  while(str[i])
    printf("%d ",str[i++]+key);

  return 0;
}

Пример:

Enter 6 letter password (all caps): ISABEL
Enter a single digit cipher key (between 2-8):7
The ciphertext is: 80 90 72 73 76 83

1 Ответ

0 голосов
/ 20 января 2019

Если я хорошо понимаю, вы хотите что-то подобное:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (void)
{
  char str[7];

  printf("Enter 6 letter password (all caps): ");

  if ((scanf("%6s", str) != 1) || (strlen(str) != 6)) {
    puts("invalid input");
    return 0;
  }

  for (int i = 0; i != 6; ++i) {
    if (!isupper(str[i])) {
      printf("'%c' is not an uppercase character\n", str[i]);
      return 0;
    }
  }

  int key;

  printf ("Enter a single digit cipher key (between 2-8):");
  if ((scanf ("%d", &key) != 1) || (key < 2) || (key > 8)) {
    puts("invalid value");
    return 0;
  }

  printf("The ciphertext is (ascii) :");
  for (int i = 0; str[i]; ++i)
    printf("%c", str[i]+key);
  putchar('\n');

  printf("The ciphertext is (codes) :");
  for (int i = 0; str[i]; ++i)
    printf("%02d", str[i]+key);
  putchar('\n');

  return 0;
}

Исполнение:

Enter 6 letter password (all caps): ISABEL
Enter a single digit cipher key (between 2-8):7
The ciphertext is (ascii) :PZHILS
The ciphertext is (codes) :809072737683
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...