Reputation: 117
When I was finishing exercise 6.2, this is what it says:
Exercise 6-2. Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter . Don’t count words within strings and comments. Make 6 a parameter that can be set from the command line.
Unfortunately I was NOT able to understand something like this
prints in alphabetical order each group of variable names that are identical in the first 6 characters
Could someone explain it a bit to me? Given the c program I am about to read is the following:
int main(int argc, char *argv[]){
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0, found = 0;
while (--argc > 0 && (*++argv)[0] == '-'){
while (c = *++argv[0]){
switch(c){
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
}
}
if (argc != 1)
printf("Usage: find -x -n pattern\n");
else
while(my_getline(line,MAXLINE) > 0){
lineno++;
if ((strstr(line, *argv) != NULL) != except){
if (number)
printf("%ld", lineno);
printf("%s", line);
found++;
}
}
return found;
}
Hope the C program to be read is not complex.
Upvotes: 1
Views: 279
Reputation: 44340
prints in alphabetical order each group of variable names that are identical in the first 6 characters
Could someone explain it a bit to me?
My understanding is that you need to parse the C-code and identify all variable names. Then you need to identify names starting with the same 6 characters and put them groups.
Simple example:
int main(void)
{
int bbbbbb2 = 42;
int aaaaaa2 = 41;
int bbbbbb1 = 43;
int aaaaaa1 = 44;
printf("%d\n", aaaaaa2*aaaaaa1 + bbbbbb2/bbbbbb1);
return 0;
}
This program has 4 variables:
bbbbbb2 aaaaaa2 bbbbbb1 aaaaaa1
There are two groups:
Group bbbbbb: bbbbbb2 bbbbbb1
Group aaaaaa: aaaaaa2 aaaaaa1
so your sorted output must be something like
Group aaaaaa
------------
aaaaaa1
aaaaaa2
Group bbbbbb
------------
bbbbbb1
bbbbbb2
To implement this I would probably start with these steps:
Upvotes: 2