How to map array of a number to the corresponding alphabet (1-a 2-b ... 26-z) in C++

2 Answers

0 votes
#include <iostream>

int main(void) {
    char abc[] = "abcdefghijklmnopqrstuvwxyz";
     
    int arr[] = {1, 12, 7, 26, 18, 20};
  
    int size = sizeof(arr)/sizeof(arr[0]); 
    for (int i = 0; i < size; i++) {
        std::cout << abc[arr[i] - 1] << " ";  // -1 for 'a' // 1-'a' ... 26-'a'
    }
}
 
 
 
/*
run:
  
a l g z r t 
  
*/

 



answered Nov 23, 2021 by avibootz
0 votes
#include <iostream>
 
int main(void) {
    int arr[] = {1, 12, 7, 26, 18, 20};
   
    int size = sizeof(arr)/sizeof(arr[0]); 
    for (int i = 0; i < size; i++) {
        std::cout << (char)(arr[i] + 'a' - 1) << " ";  // -1 for 'a' // 1-'a' ... 26-'a'
    }
}
  
  
  
/*
run:
   
a l g z r t 
   
*/
  

 



answered Nov 23, 2021 by avibootz

Related questions

...