-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-3.c
81 lines (59 loc) · 2.06 KB
/
3-3.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Exercise 3-3.
Write a function expand (s1, s2)
that expands shorthand notations like a - z in the string s 1 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-zO-9 and -a-z.
Arrange that a leading or trailing - is taken literally.
*/
#include <stdio.h>
#include <string.h>
#define MAXNUMBER 100 /* number of array */
void expand1(char s1[], char s2[]);
int main (){
char s1[] = "1-5a-c";
char s2[MAXNUMBER];
printf ("char s1 is %s \n", s1);
expand1(s1, s2);
printf ("char s2 is %s \n", s2);
}
void expand1(char s1[], char s2[]){
int j, n, len;
int c = 0; /* charactor already assigned on */
int whattype = 0; /* this indicate type either 1-digit, 2-capital letter, 3-small letter */
int i = 0;
n = strlen (s1);
while(i < n){
if ((s1[i] >= '0' && s1[i] <= '9') && (s1[i+2] >= '0' && s1[i+2] <= '9')){
whattype = 1;
}
else if ((s1[i] >= 'A' && s1[i] <= 'Z')&& (s1[i+2] >= 'A' && s1[i+2] <= 'Z')) {
whattype = 2;
}
else if ((s1[i] >= 'a' && s1[i] <= 'z') && (s1[i+2] >= 'a' && s1[i+2] <= 'z')){
whattype = 3;
}
if ((whattype==1 || whattype==2 || whattype==3 )&& s1[i+1] == '-' && s1[i] < s1[i+2]){
for (j=i+c, len = s1[i+2]-s1[i] +1; j-i-c < len ; j++ ){
s2[j] = s1[i]+(j-i-c);
}
}
/* *
else if (whattype==2 && s1[i+1] == '-' && s1[i] < s1[i+2]){
for (j=i+c, len = s1[i+2]-s1[i] +1; j-i-c < len ; j++ ){
s2[j] = s1[i]+(j-i-c);
}
}
else if (whattype==3 && s1[i+1] == '-' && s1[i]<s1[i+2]){
//printf ("yay\n");
for (j=c, len = s1[i+2]-s1[i] +1; j-c < len ; j++ ){
s2[j] = s1[i]+(j-c);
}
}
*/
c +=len;
i += 3;
if (s1[i] == '\0'){
s2[j] = s1[i];
}
}
}