首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从最终输出密文中删除空格?

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

Stack Overflow用户
提问于 2019-01-20 23:20:51
回答 1查看 39关注 0票数 0

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

代码语言:javascript
复制
#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;
}

示例:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-01-20 23:47:31

如果我很清楚你想要这样的东西:

代码语言:javascript
复制
#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;
}

执行:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54277904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档