J
Jason
hello everyone! recently i've been working through K&R's The C
programming language, and i just finished exercise 3.3, since my
solution is shorter than the "suggested solution" online, i want to
see if mine is correct to spec.
The question 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.
One question before going on to the code, i interpret their "leading
or trailing - is taken literally" by that for example, if i input -a,
the program would expand from '-' all the way to a based on ascii
character set, and similar thing would happen for trailing -. However,
there are certain ambiguity with the following sample input: "-a-z",
my program would interpret that as from '-' to 'a' then from '-' to
'z', doesn't seem right, but that's the simplist way of doing it.
Anyways, here is my code, blast away!
#include <stdio.h>
/*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, j, queue, start, end;
j = queue = start = end = 0;
for(i = 0; s1 != '\0'; i++)
{
if(s1 == '-')
{
if(queue != 0){
start = queue;
}else{
start = s1; /* handles leading '-' */
}
if(s1[i+1] == '\0'){
end = s1; /* handles trailing '-' */
}else{
end = s1[++i];
}
for(; start <= end; start++)
{
s2[j++] = start;
}
end = 0;
start = 0;
}
else
{
queue = s1;
}
}
}
main()
{
char str1[] = "a-zA-Z";
char str3[] = "-a-zA-Z";
char str2[1000];
int i;
for(i = 0; i<1000; i++)
{
str2 = 0;
}
expand(str3, str2);
printf("Input: %s\n", str1);
printf("Output: %s\n", str2);
system("pause");
}
Any comments would be much appreciated!
programming language, and i just finished exercise 3.3, since my
solution is shorter than the "suggested solution" online, i want to
see if mine is correct to spec.
The question 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.
One question before going on to the code, i interpret their "leading
or trailing - is taken literally" by that for example, if i input -a,
the program would expand from '-' all the way to a based on ascii
character set, and similar thing would happen for trailing -. However,
there are certain ambiguity with the following sample input: "-a-z",
my program would interpret that as from '-' to 'a' then from '-' to
'z', doesn't seem right, but that's the simplist way of doing it.
Anyways, here is my code, blast away!
#include <stdio.h>
/*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, j, queue, start, end;
j = queue = start = end = 0;
for(i = 0; s1 != '\0'; i++)
{
if(s1 == '-')
{
if(queue != 0){
start = queue;
}else{
start = s1; /* handles leading '-' */
}
if(s1[i+1] == '\0'){
end = s1; /* handles trailing '-' */
}else{
end = s1[++i];
}
for(; start <= end; start++)
{
s2[j++] = start;
}
end = 0;
start = 0;
}
else
{
queue = s1;
}
}
}
main()
{
char str1[] = "a-zA-Z";
char str3[] = "-a-zA-Z";
char str2[1000];
int i;
for(i = 0; i<1000; i++)
{
str2 = 0;
}
expand(str3, str2);
printf("Input: %s\n", str1);
printf("Output: %s\n", str2);
system("pause");
}
Any comments would be much appreciated!