-3

this program accepts an integer n, which is the number of letter till which letter should be printed. The pattern of the letters is as follows: the first is "a1", the second is "b1", the third is "c1", and the 26th is "z1", then the 27th roll number is "a2" and the pattern continues.should we try to use ASCII values? example:

n = 10

output: a1, b1,c1, d1,e1, f1,g1, h1,i1, j1

n = 28

output: a1, b1,c1, d1,e1, f1,g1, h1,i1, j1, ..., z1, a2, b2

2 Answers2

2

A slightly condensed version that operates on the same principal could be:

#include <stdio.h>

int main (void) {
  
  int n,              /* your number 10, 28, etc... */
      suffix = 0;     /* number after a(), b(), c(), ... */
  
  fputs ("enter n: ", stdout);    /* prompt for n */
  
  /* read/validate positive int value */
  if (scanf ("%d", &n) != 1 || n <= 0) {
    fputs ("error: invalid, zero or negative integer input.\n", stderr);
    return 1;
  }
  
  fputs ("output : ", stdout);    /* prefix for output */
  
  while (n > 0) {     /* while n greater than 0 */
    /* loop i from 0 to n or 26 (whichever is less) */
    for (int i = 0; i < (n > 26 ? 26 : n); i++) {
      /* if i not 0 or suffix not 0, add ", " before next output */
      printf (i || suffix ? ", %c%d" : "%c%d", 'a' + i, suffix + 1);
    }
    n -= 26;          /* subtract 26 fron n */
    suffix += 1;      /* add 1 to suffix */
  }
  
  putchar ('\n');     /* tidy up with final newline */
}

(Note: both i and suffix loop from 0 instead of 1 which allows a test of whether suffix or i have been used, e.g. 0 == false, 1 == true. That way you can control whether the ',' is output by checking whether i or suffix have been set yet)

Example Use/Output

$ ./bin/alphabetloop
enter n: 10
output : a1, b1, c1, d1, e1, f1, g1, h1, i1, j1

or

$ ./bin/alphabetloop
enter n: 28
output : a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1, q1, r1, s1, t1, u1, v1, w1, x1, y1, z1, a2, b2
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

One way to solve the problem would be to print all codes in a nested loop, until one of the following happens:

  1. the number of codes printed reaches n
  2. you have printed all 234 codes from "a1" to "z9" (which would only occur if n is at least 234)
#include <stdio.h>

void print_codes( int n )
{
    //handle one digit per loop iteration
    for ( int i = 1; i < 10; i++ )
    {
        //handle one letter per loop iteration
        for ( int j = 0; j < 26; j++ )
        {
            //check whether we have already printed the
            //desired number of codes
            if ( n-- <= 0 )
                return;

            //add spacing between codes, if necessary
            if ( i != 1 || j != 0 )
                printf( ", " );

            //print the actual code
            printf( "%c%d", 'a' + j, i );
        }
    }
}

int main( void )
{
    printf( "For n == 10:\n" );
    print_codes( 10 );
    printf( "\n" );

    printf( "For n == 28:\n" );
    print_codes( 28 );
    printf( "\n" );
}

This program has the following output:

For n == 10:
a1, b1, c1, d1, e1, f1, g1, h1, i1, j1
For n == 28:
a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1, q1, r1, s1, t1, u1, v1, w1, x1, y1, z1, a2, b2
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • You got fancy and wrote a separate function, I just took input twice `:)` – David C. Rankin Apr 09 '22 at 06:14
  • @DavidC.Rankin: I didn't want to do all of OP's homework, so I thought I'd at least leave the "input an integer" part to OP. Also, I don't like using the function `scanf` for user input, and I was too lazy to write a `fgets`/`strtol` solution. ;-) The best solution would probably have been to take over my function `get_int_from_user` from the second code snippet of [this answer of mine](https://stackoverflow.com/a/69636446/12149471), but that would have increased the length of my answer by a factor of 4. :-( – Andreas Wenzel Apr 09 '22 at 06:50
  • I ducked the length issue and just tossed a couple ternarys in to avoid the differing conditionals (and hopefully to prompt a curious inquiry from the OP so additional learning may occur...) – David C. Rankin Apr 09 '22 at 06:55