c - 我如何编写一个程序,根据下面的文本对仅以大写字母书写的文本进行编码?

标签 c

  1 2 3 4 5 6  
1 A B C D E F
2 G H I J K L
3 M N O P R S
4 T U V Y Z W
5 X 1 2 3 4 5
6 6 7 8 9 ? !
7 - + * 

我想这样做。

你好 1234 等效 -> 221526263352535455

这是我得到的输出。

636465

我从一个简单的逻辑开始,但无法继续。

我的代码。

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

int main(){
    int i,j;
    
  
    char string[20];
  char b[7][6]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U'
  ,'V','Y','Z','W','X','1','2','3','4','5','6','7','8','9','?','!','-','+','*'};
  printf("please enter an input");
  scanf("%s",&string);
  
  
  for(i=0;i<7;i++){
    for(j=0;j<6;j++){
        if(string[i]==b[i][j])
            printf("%d%d",i,j);
            
      }
  }
  
  return 0;
}

最佳答案

我创建了一个包含 256 个元素的 char 数组,以说明所有 ASCII 字符。我将字符的相应值存储在数组中。 例如:A-->11,alpha_num['A'] = 11。同样,我分配了所有剩余的字符。

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

int main()
{
    int i = 1, j = 1;
    int k = 0;
    char alpha_num[256];
    char string[20];
    printf("Please enter an input\n");
    scanf("%s", string);

    for (char c = 'A'; c <= 'Z'; c++)
    {
        if (j <= 6 && i <= 7)
        {
            alpha_num[c] = i * 10 + j;
            j++;
        }
        if (j > 6)
        {
            j = 1;
            i++;
        }

        if (i > 7)
            i = 1;
    }

    i = 5;
    j = 2;
    for (char c = '1'; c <= '9'; c++)
    {
        if (j <= 6 && i <= 7)
        {
            alpha_num[c] = i * 10 + j;
            j++;
        }
        if (j > 6)
        {
            j = 1;
            i++;
        }

        if (i > 7)
            i = 1;
    }

    alpha_num['?'] = 65;
    alpha_num['!'] = 66;
    alpha_num['-'] = 71;
    alpha_num['+'] = 72;
    alpha_num['*'] = 73;

    for (int i = 0; i < strlen(string); i++)
    {
        printf("%d", alpha_num[string[i]]);
    }

    return 0;
}

输出是:

Please enter an input
HELLO1234
221526263352535455

关于c - 我如何编写一个程序,根据下面的文本对仅以大写字母书写的文本进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65441075/

相关文章:

c - 15 拼图程序,卡在上下命令上

c - 将结构发送到 C 中的函数

c - 使用 C 语言的套接字编程,使用 select() 函数

c - 大输入数频程序

c - 为什么在不同文件中定义的函数可以访问在该文件中定义的变量而无需使用 extern?

c - 指针作为调用 scanf 的函数的参数

c - 字节大小字段的结构填充和对齐?

c - 编程 Linux 应用程序以同时播放多个声音

c - "make target"在 "make clean"之后不工作

c - gets() 没有按预期工作