Home > Net >  I have to print the position of an alphabet in c language
I have to print the position of an alphabet in c language

Time:01-05

basically if the input is "1" i need it to say a if the input is "2" it needs to say b and so on and so forth

i have tried if else trees and they are L O N G so please help me

CodePudding user response:

One way is to have a list of chars storing the alphabet and just index out the values

#include <stdio.h>

int main()
{
    int number;
    char alpha[] = "abcdefghijklmnopqrstuvwxyz";
    scanf("%d", &number);
    printf("%c",alpha[number-1]);// because when indexing, the first "spot" is 0 but you want 1 to return a not so the -1 readjust that

    return 0;
}

CodePudding user response:

another solution:

#include <stdio.h>

int main()
{
    int number;
    scanf("%d", &number);
    printf("%c",'a' number-1);

    return 0;
}
  •  Tags:  
  • Related