# Expansion

This is a discussion on Expansion within the C Programming forums, part of the General Programming Boards category; Hi, I'm working through the problems in K&R and I'm having difficulty with a particular one. Code: /*Write a function ...

1. ## Expansion

Hi, I'm working through the problems in K&R and I'm having difficulty with a particular one.

Code:
```/*Write a function expand(s1,s2) that expands shorthand notations like a-z in the
string s1 into the equivalent complete list abc...xyz in s2. Allow for letters of either case and digits,
and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arrange that a leading or trailing -
is taken literally.*/

void expand (char s1[], char s2[]){

int i = 0,j = 0;

while(s1[i] != '\0'){
if(( i == 0 || s1[i+1] == '\0') && s1[i] == '-'){   //1st or last char is a "-"
s2[j++] = s1[i];
else if(s1[x] == '-'){
if(s1[i-1] >= '0' && s1[i-1] <= '9'){ // check if the left char is a 0 or a 9 eg. 0-9, 9-0
if(s1[i+1] >= '0' && s1[i+1] <= '9'){ //check the same for the right char
if(s1[i-1] <= s1[i+1]){
for(q = s1[i-1]; q <= s1[i+1]; q++)
s2[j++] = q;}```
So far I think that this code checks if the leading characted is a "-" and copies it and checks if the string starts with a 0 or a 9 on and ends with a 0 or a 9. However, I'm not confident at all at what I've done and any criticisms and pointers would be much welcomed. I realize I could use pointers, but i'm avoiding it just because it hasn't come up in the book quite yet. Even though their solution does use pointers.

2. I suggest you indent you code properly first.

As you have no side effect in your example, it's pretty easy.
I assume s2 has to be allocated beforehand (this is not really convenient, how much memory should be allocated for it?).
Code:
```#include <assert.h>
...
int main(void) {
char *in = "0-9";
char out[512]; /* allocate a big chunk of memory, how much is needed? */
char *expected = "0123456789";
expand(in, out);
assert(!strcmp(out, expected));
}```
This is just a draft example, organize the tests into functions according to your needs.