C program to prints out letters in alphabet order

Helo want to create program that print out letters in alphabetical order
in input number defines how much numbers it should print out
Input
a 3
c 10
E 5
x 30

Output should look like
z='a' a len=3 straight is : abc
z='c' a len=10 straight: cdefghijkl
z='E' a len=5 straight: EFGHI
z='x' a len=30 straight: xyzabcdefghijklmnopqrstuvwxyza


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>


void straight(char *str, char z, int len)
{
  
  str[0]=z;
  int i;
for (i=0,i<len,i++)
  {
  str[i]=z;
  z++;
    if (z=='z'+1)||(z=='Z'+1) ;
      z=26;
      
      
  }

 str[len]=0;
}



int main(void)
{
  char z[2], *buf;
  int n;
  

  while (scanf("%s %d", z, &n) == 2)
  {
    buf = malloc(n+1); 
    straight(buf, z[0], n);
  	printf("%s\n", buf);
    free(buf); 
  }
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

const char* lowercase = "abcdefghijklmnopqrstuvwxyz";
const char* uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int str_size = 26;


int find(const char* str, char what, int size)
{
    int pos = 0;
    while(pos < size && str[pos] != what)
        ++pos;
    return pos;
}

void straight(char z, int len)
{
    const char* str = isupper(z) ? uppercase : lowercase;
    int pos = find(str, z, str_size);
    if(pos == str_size) return;
    for(int i = pos; i < pos + len; ++i)
        printf("%c", str[i % str_size]);
}

int main(void)
{
    char z;
    int len;
    while(scanf(" %c %d", &z, &len) == 2) {
        straight(z, len);
        putchar('\n');
    }
    return 0;
}
a 3
abc
c 10
cdefghijkl
E 5
EFGHI
x 30
xyzabcdefghijklmnopqrstuvwxyza

Last edited on
Topic archived. No new replies allowed.