Home > Software design >  Printing string inside of an integer array
Printing string inside of an integer array

Time:05-16

I am trying to print a string in between integers. I have an integer array and, on every 10th position, I am trying to print "ten" instead of "10", "twenty" instead of "20" and so on until 100.

Is there any way of doing it but still keeping the original integer array afterwards.

Here is something I have tried:

  int A[limit];
  for (int i = 0, j = 1; i <limit; i  , j  ){
    A[i] = j;
    if(A[i] == 10){
      A[i] = 1;
      printf("%s", "ten");
      j=1;
    }
     
    printf("%d", A[i]);
  }

I wanted to print this:

1 2 3 4 5 6 7 8 9 ten 1 2 3 4 5 6 7 8 9 twenty 1 2 3....

CodePudding user response:

Here is a simple solution:

    for (int i = 1; i <= 100; i  ) {
        switch (i) {
          case 10:  printf("ten ");     break;
          case 20:  printf("twenty ");  break;
          case 30:  printf("thirty ");  break;
          case 40:  printf("forty ");   break;
          case 50:  printf("fifty ");   break;
          case 60:  printf("sixty ");   break;
          case 70:  printf("seventy "); break;
          case 80:  printf("eighty ");  break;
          case 90:  printf("ninety ");  break;
          case 100: printf("one hundred ");  break;
          default:  printf("%d ", i % 10);   break;
        }
    }

Or using an array:

    const char *tens[] = {
          "zero", "ten", "twenty", "thirty", "forty",
          "fifty", "sixty", "seventy", "eighty", "ninety",
          "one hundred" };
    for (int i = 1; i <= 100; i  ) {
        if (i % 10 == 0) {
            printf("%s ", tens[i / 10]);
        } else {
            printf("%d ", i % 10);
        }
    }

CodePudding user response:

Keeping the logic much the same as in your code (especially the resetting of the stored/output numbers whenever a multiple of 10 is reached), we can rewrite the code as shown below:

#include<stdio.h>

#define limit 100

int main()
{
    const char* tens[] = { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "a hundred" };
    int A[limit];
    for (int i = 0, j = 1; i < limit; i  , j  ) {
        A[i] = j;
        if (A[i] % 10 == 0) {
            A[i] = 1;
            printf("%s ", tens[i/10]);
            j = 0; // Reset to ZERO because of the loop increment
        }
        else { // Don't print number if we have shown the word version
            printf("%d ", A[i]);
        }
    }

    return 0;
}

The main idea is to create an array of the words to be printed when appropriate; that will be whenever the remainder of a division by 10 is zero (the A[i] % 10 == 0 condition). The index of which word to print will be given by dividing the running i value by 10 (making use of the fact the integer division truncates).

Also note some other changes I have made:

  1. The j value should be reset to zero (not 1) when a word is printed, because it will be subsequently incremented (to 1) at the end of that loop.
  2. I have moved the 'normal' integer printing into an else block, because your desired output doesn't print the number when it prints the word.

However, if you want to keep the original number sequence (i.e. 1 thru 100) in the array, then you can remove the j variable completely and print the value of A[i] % 10 when displaying the numerical value:

#include<stdio.h>
#define limit 100

int main()
{
    const char* tens[] = { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "a hundred" };
    int A[limit];
    for (int i = 0; i < limit; i  ) {
        A[i] = i   1;
        if (A[i] % 10 == 0) {
            printf("%s ", tens[i/10]);
        }
        else { // Don't print number if we have shown the word version
            printf("%d ", A[i] % 10);
        }
    }
    // Original array keeps the proper values ...
    printf("\n");
    for (int i = 0; i < limit; i  ) {
        printf("%d ", A[i]);
    }
    return 0;
}
  • Related