如何根据下面的文本对仅用大写字母书写的文本进行编码

  • 本文关键字:文本 大写字母 书写 编码 何根 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 - + * 

我想那样做。

HELLO 1234等效物->22152626335535455

这是我得到的输出。

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 inputn");
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

我选择了简短而简单的

(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') = '';

size_t len = strlen(input);
char output[2*len +1];

encode(input, output, matrix);
printf("nEncoded String: %sn", output);
return 0;
}

您可以对string字符进行迭代,并检查当前字符是否在b中找到。如果找到,您只需打印row+1column+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);   
}
}
#include <stdio.h>
struct CodeList {
int code;   
int key;
};
int main()
{
struct CodeList  list[6*7];
int i = 1;
int y =1;
int x =1;
for (char c = 'A'; c <='Z'; c++){
if (x  > 6) {
x =1;
y++;
}
if (y  > 7) {
y =1;
}
list[i].key = c;  
list[i].code = (y * 10) + x;
i++;
x++;

}

//search
char s = 'D';

for (i =0; i< 6*7; i++){
if(s == list[i].key){
printf("code %i", list[i].code);// 14
}
}
}

顺便说一下,您忽略了字母Q。

最新更新