3
 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 - + * 

I want to do that.

HELLO 1234 equivalent -> 221526263352535455

It is output that I got.

636465

I started with a simple logic, but could not continue.

My codes.

#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; } 
10
  • No need of & in scanf. Commented Dec 24, 2020 at 16:52
  • I've made a correction but still can't get the output I want. Commented Dec 24, 2020 at 16:54
  • 1
    it would be easier if you change the b[][] to be indexed by letters, like b['H'] = 22 ... Commented Dec 24, 2020 at 16:54
  • Check the return value of scanf(). Commented Dec 24, 2020 at 16:55
  • 1
    Yes, but how else can I print the numbers in the table side by side? For example, how can I print the row value of matrix 1 and column value 5 for the letter 'E'? Commented Dec 24, 2020 at 16:59

4 Answers 4

2

I created an char array of 256 elements, to account for all ASCII characters. I stored corresponding value of the character in the array. For example: A-->11, alpha_num['A'] = 11. Similarily, I assigned all the remaining characters.

#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; } 

The output is :

Please enter an input HELLO1234 221526263352535455 
Sign up to request clarification or add additional context in comments.

Comments

1

I went for short and simple:

(IDEOne Link)

#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; } 

Comments

0

You can iterate over string characters and check if the current char is found in b. If it's found you can just print the row+1 and the 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); } } 

Comments

0
 #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 } } } 

By the way, you ignored the letter Q.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.