Home > Software engineering >  how to print letter based on input integer given buy the user?
how to print letter based on input integer given buy the user?

Time:04-09

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

CodePudding user response:

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

CodePudding user response:

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 )
{
    for ( int i = 1; i < 10; i   )
    {
        for ( int j = 0; j < 26; j   )
        {
            if ( n-- <= 0 )
                return;

            if ( i != 1 || j != 0 )
                printf( ", " );

            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
  •  Tags:  
  • c
  • Related