I want to display the alphabet one letter at a time using a loop in C language.

Asked 2 years ago, Updated 2 years ago, 36 views

I'd like to make a sentence in C language that results in the following, but I can't do it well, so I'd appreciate it if you could let me know.

Expect Results:

A
AB
ABC Corporation
ABCD

Current source code:

#include<stdio.h>
int main (void)
{
    inti,j;
    for (i=65; i<=90;i++)
    {
        for (j=1; j<=i;j++)
            
            printf("%c", i);
            
        printf("\n");
    }
    return(0);
}

c

2022-09-30 20:23

4 Answers

Below is how to use the % .*s (width precision) format of printf(3).

#include<stdio.h>

int main(void){
  char string [ ] = "ABCD";
  intsz=sizeof(string);

  for(inti=1;i<sz;i++){
    printf("%.*s\n", i, string);    
  }

  return 0;
}

printf(3)

Precision

An optional precision, in the form of a period('.') followed by an optional decimal digit string.Instead of a decimal digit string one may write"*" or "*m$" (for some decimal integr) to specify that the precision is given in the next argument, the amount, the amount, the amount.        :

...the maximum number of characters to be printed from a string for s and Sconversations.


2022-09-30 20:23

If possible, try to meet the exact specifications according to the title.

- The original code is A to Z
- EBCDIC/EBCDIK contiguous
from 'A' to 'Z' in ASCII, but not EBCDIC/EBCDIK contiguous
- Include Double Loop

Even in the EBCDIK system, if you want to make it as simple as expected, you should list the characters and change the subscripts. it is customary for loops to start with 0, so the outer loop is probably for(i=0;i<26;++i)Inside loop more than the inner loop is interesting>

#include<stdio.h>
# define elements of(n)(sizeof(n)/sizeof(0[n]))
int main() {
    const char alphabets[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // try resize me
    // must exclude terminating '\0'
    for(inti=0;i<elementsof(alphabets)-1;++i){
        for(int j=0;j<=i;++j){
            putchar(alphabets[j]);
        }
        putchar('\n');
    }
}

Once you get used to it, you immediately realize that you don't need a double loop when alphabets[] comes to mind.

The proposed source code does not work as expected because printf("%c", i);'s i has not changed in the inner loop, and the outer loop is moving the end position, but the inner loop has the wrong start position.


2022-09-30 20:23

It's ASCII that 65 is 'A' and 'A'~'Z' are continuous.
I think it's better to avoid writing because it depends on the processing system.
I think the printf function %s precision specification is easy to use for this specification.

#include<stdio.h>
# include <string.h>

int main()
{
    const char*ps = "ABCD";

    intlen=strlen(ps);
    for(inti=0;i<len;++i){
        char format[10]="";
        snprintf(format, size of format, "%%.%ds\n", i+1);
        printf(format,ps);
    }
    return 0;
}


2022-09-30 20:23

The key is how many characters you want to print from A.

#include<stdio.h>
int main (void)
{
    for (inti=1; i<=26;i++)
    {
        for(int j=0;j<i;j++){
             printf("%c", 'A'+j);
        }

        printf("\n");
    }
    return(0);
}

Depending on the execution environment (character code), A is not necessarily next to B, so I think it's better to do it like someone else.
I think the code above is appropriate for class.

// Non-environmental code
# include <stdio.h>
int main (void)
{
    const char alphabets[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    for (inti=1; i<=26;i++)
    {
        for(int j=0;j<i;j++){
             printf("%c", alphabets[j]);
        }

        printf("\n");
    }
    return(0);
}


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.