首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据下面的文字,我如何编码一个只以大写字母写成的文本的程序?

根据下面的文字,我如何编码一个只以大写字母写成的文本的程序?
EN

Stack Overflow用户
提问于 2020-12-24 16:44:54
回答 4查看 93关注 0票数 3
代码语言:javascript
复制
  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 - + * 

我想这么做。

Hello1234等效-> 221526263352535455

我得到的是输出。

636465

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

我的密码。

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

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-12-24 17:14:11

您可以迭代string字符,并检查当前字符是否在b中找到。如果找到了,你只需打印row+1column+1

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>

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','?','!','-','+','*'};

void findRowCol(char c,int *row,int *col){// change row,col to the founded index, if not found to -1
     *row=-1;
     *col=-1;
     for(int i=0;i<7;i++){
         for(int j=0;j<6;j++){
            if(c==b[i][j]){
              *row=i;
              *col=j;
          }
       }
    }
}

int main()
{
    char string[20] = "HELLO1234";

    for(int i=0;i<strlen(string);i++){
       int row,col;
       findRowCol(string[i],&row,&col);
       if(row!=-1) printf("%d%d",row+1,col+1);   
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-12-24 17:36:32

我创建了一个由256个元素组成的char数组,以解释所有的ASCII字符。我将字符的对应值存储在数组中。例如:a->11,alpha_num['A'] = 11。类似地,我分配了所有剩下的字符。

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

产出如下:

代码语言:javascript
复制
Please enter an input
HELLO1234
221526263352535455
票数 2
EN

Stack Overflow用户

发布于 2020-12-24 18:39:58

我追求的是简洁和简单:

(IDEOne链路)

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>

void encode(char* input, char* output, char* matrix)
{
    while(*input)
    {
        size_t idx = strchr(matrix, *input++) - matrix;
        *output++ = '1' + idx / 6;
        *output++ = '1' + idx % 6;
    }
    *output = 0;
}

int main(void) {
    char matrix[]="ABCDEFGHIJKLMNOPRSTUVYZWX123456789?!-+* ";
    char input[50];
    
    printf("please enter an input");
    fgets(input, 50, stdin);
    *strchr(input, '\n') = '\0';
    
    size_t len = strlen(input);
    char output[2*len +1];
    
    encode(input, output, matrix);
    printf("\nEncoded String: %s\n", output);
    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65441075

复制
相关文章

相似问题

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