I got a question one of my friends asked me to make a program that you type in the number 1-27 on the command line, and get the corresponding letter for it. Like if you enter 1 as the arguement you would get an A as an output, and if you type in 2, and get a B for the output and so on.

Dani AI

Generated

wanted a small command‑line program that turns a number into a letter. The usual, unambiguous mapping for English is 1 → A through 26 → Z; anything outside that range should be handled explicitly (error, wrap, or a defined extra symbol). In this thread asked for the OP's code and warned about doing someone else's homework; posted a working idea but it contains portability and correctness problems that are worth fixing.

Problems to avoid from 's example: use of conio.h/clrscr()/getch() is nonstandard and platform specific; void main() is not portable—use int main(...); redeclaring atoi with the wrong signature and implementing a single‑digit parser is incorrect and masks the standard library function; there is no check for out‑of‑range input. Relying on magic constants (like +64) works but is less clear than using 'A' + n - 1.

A compact, portable pattern is: parse the argument with strtol, check errno and the end pointer to ensure a whole valid integer, validate the range (1..26), then compute the character with 'A' + value - 1. Example implementation:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s N  (N = 1..26)\n", argv[0]);
        return 1;
    }

    char *end;
    errno = 0;
    long val = strtol(argv[1], &end, 10);
    if (errno || *end != '\0' || val < 1 || val > 26) {
        fprintf(stderr, "Invalid input: must be an integer 1..26\n");
        return 1;
    }

    putchar((char)('A' + (int)(val - 1)));
    putchar('\n');
    return 0;
}

Compile with gcc -std=c11 -Wall -Wextra (or your platform's compiler). For different behavior (lowercase letters, a special mapping for 27, wrapping) explicitly define that requirement and validate accordingly.

Recommended Answers

All 4 Replies

I got a question one of my friends asked me to make a program that you type in the number 1-27 on the command line, and get the corresponding letter for it. Like if you enter 1 as the arguement you would get an A as an output, and if you type in 2, and get a B for the output and so on.

It would help if you supplied your code to see where you are having problems.

I don't have any code for it, he was just wanted me to do some code for him, sense I am out fo work for a couple of weeks, with a broke leg, and have nothing to do, but I still do work for job at home, and don't have the time to do this for him.

so tell him you don't have the time to do his homework for him, just like we don't have the time either...

I think this code will help you out

# include<stdio.h>
# include<conio.h>
char change(int); /* Function prototype */
void main()
{   int num;
    clrscr();
    printf("Enter any number : ");
    scanf("%d",&num); /* getting the no from user */
    printf("%c",change(num));
    getch();

}

char change(int n)
{   char k;
    k = (char)n;
    k = k+64;
    return k;
}

for command line arguement

# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
char change(int); /* Function prototype */
int atoi(char);
void main(int count,char *argv[])
{    if(count != 2)
      {  puts("INVALID COMMAND");
          exit(0);
      }
      int num;
     num = atoi(argv[1]);
      printf("%c",change(num));
     getch();
}

char change(int n)
{   char k;
     k = (char)n;
     k = k+64;
    return k;
}
int atoi(char c)
{  return c - '0';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.