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
我从一个简单的逻辑开始,但不能继续。
我的密码。
#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;
}发布于 2020-12-24 17:14:11
您可以迭代string字符,并检查当前字符是否在b中找到。如果找到了,你只需打印row+1和column+1
#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);
}
}发布于 2020-12-24 17:36:32
我创建了一个由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发布于 2020-12-24 18:39:58
我追求的是简洁和简单:
(IDEOne链路)
#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;
}https://stackoverflow.com/questions/65441075
复制相似问题