# Thread: Expand

1. ## Expand

Hi! I've been working through the C Programming Language Second Edition, and I'm stumped on one of the exercises. Exercise 3-3 has me baffled. The task is as follows:
Exercise 3-3. 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.

So far I've written the expand function as:
Code:
```void Expand(char s1[], char s2[]) {
int i = 0;
int y = 0;
while((s2[i++] = 0) != '\0') /* clear  s2 */
;
for(i = 0; s1[i] != '\0'; ) {
int start = s1[i++];
/* advance i until a new type is found to handle cases like a-b-c-d */
if(isdigit(start)) {
for( ; isdigit(s1[i]) || s1[i] == '-'; i++)
;
}
else if(islower(start)) {
for( ; islower(s1[i]) || s1[i] == '-'; i++)
;
}
else if(isupper(start)) {
for( ; isupper(s1[i]) || s1[i] == '-'; i++)
;
}
else {		   /* if they are not the same type, exit the function */
printf("Invalid character\n");
return;
}
int end = s1[i-1];
/* check if start and end are the same type, and don't go over bounds */
if(isdigit(start) && isdigit(end)) {
while(start <= end)
s2[y++] = start++;
}
else if(islower(start) && islower(end)) {
while(start <= end)
s2[y++] = start++;
}
else if(isupper(start) && isupper(end)) {
while(start <= end)
s2[y++] = start++;
}
else {
printf("Invalid character");
return;
}
}
return;
}```
However, for some reason if I pass it a string like a-z0-9, it is adding on -9 at the end of it. My output would be: abcdefghijklmnopqrstuvwxyz0123456789-9

Can someone please help me diagnose the problem, I'm completely baffled.

2. You need to nul terminate your s2 string when you are done sticking things in it.

Quzah.

3. Thank you, that fixed it. I don't understand where the extra characters were coming from however, could you please explain?

4. If you don't terminate your string with a '\0', printf will continue past the end of the string until it finds one somewhere else. In your case, I think it is picking up the remnants of the input string, ie. the last few characters of "a-z0-9". But this behaviour is not guaranteed; it depends on what is stored where in memory. When I tried it I got several lines of gibberish.

5. Originally Posted by ASCII
Thank you, that fixed it. I don't understand where the extra characters were coming from however, could you please explain?
Alongside H's serious explanation, here is a silly joke that illustrates the point .
Two strings walk into a bar and sit down.
The bartender says, “So what’ll it be?”
The first string says, “I think I’ll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu”
“Please excuse my friend,” the second string says, “He isn’t null-terminated.”

6. That joke doesn't really work in C, because you aren't a string if you aren't nul terminated. I know it's supposed to be a C joke, but it just bug people like me.

/buzzkill

Quzah.

7. Originally Posted by quzah
That joke doesn't really work in C, because you aren't a string if you aren't nul terminated. I know it's supposed to be a C joke, but it just bug people like me.

/buzzkill

Quzah.
Ok.. modification.. [Two const char* `s walk into,....~] but the string version is much more repeatable in human company...

8. A string and a dude that you think is a string walk into a bar...

Quzah.

9. Originally Posted by quzah
A string and a dude that you think is a string walk into a bar...
Quzah.
A guy with a sense of humour and Quzah walk into a bar...

10. Originally Posted by CommonTater
A guy with a sense of humour and Quzah walk into a bar...
A guy with a lousy sense of humor and Quzah walk into the bar...

I've got a great sense of humor. That joke just wasn't funny.

Quzah.

11. Originally Posted by quzah
I've got a great sense of humor.
Notwithstanding evidence to the contrary...

That joke just wasn't funny.
Ok... no argument on that part.

12. Originally Posted by quzah
That joke just wasn't funny.
Of course it wasn't.
Only totally pointless jokes can be funny.

Popular pages Recent additions