Thread: 97 -> a, 98 -> b, ..., 122 -> z

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    97 -> a, 98 -> b, ..., 122 -> z

    Hi,

    I study C language. As an exercise I decided to write a program, that writes all 26 letters of english alphabet on one line.

    I know that in ASCII:
    a...97
    b...98
    ........
    z...122

    I know how to make it using for loop, but I don't know how to convert 97 in dec to a character. I was looking for some functions (I have found atoi(), sscanf(), sprintf()) but I wasn't able to finish it. Could you please help me?

    Code:
    main()
    
    {
          
    int i;
    char s[1];
    
    for(i=97;i<123;i=i+1){
                      s=atoi(i);          /* I think here is the problem part */
                      printf("%c",s);
    
                      
                      }
          
    getch();      
    }
    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're going to smack yourself on the forehead when I tell you.

    Ready?

    Just
    Code:
    printf("%c", theNumber Or Variable Holding the number Goes Here);
    OK, go smack yourself!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    for ( i = 'a' ; i <= 'z' ; i = i + 1 )
    Also works on the ASCII character set.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    s=atoi(i);
    atoi is a function which converts string to an integer. This would work in your case. What you just have to do is to print integer as char in the printf statement. As Adak shown. Between getch is a non-standard function. Use getchar instead and also main should return a value int.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  4. Sort by 97, 98, 99, 00, 01, 02 etc.
    By JLan in forum C Programming
    Replies: 7
    Last Post: 12-01-2003, 02:18 AM