如何从最终输出密文中删除空格?

时间:2019-01-20 15:20:51

标签: c arrays string encryption

如何删除密文之间的空格,使其打印为单行数字?输出示例:## ## ## ## ## ##> ############

#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 :(得分:1)

如果我很了解您想要这样的话:

#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