1. ## Dissecting a string...

Well I have an assignment in which I need to first ask a user for an amount of names to input, allocate the memory needed for the amount of strings, then sort them by the choice of the user with either last, first, or middle.
names are entered in format
last,first,middle

I did all the first part of the program.. I would put up my code but Im doing it all on a different machine ;P.

anyway I can sort by last name, and I had sorting by first name working for a bit, then the middle started crashing the program when I entered in a 3 to show i wanted to sort by middle name. after that the first name no longer worked.

ill try to post off what i remember of what i did.

the user inputs into num for number of strings
and i had names as the array for strings...

z = 0;
a = 0;
while(names[y][z] != ',')
z = z + 1;
while(names[y+1][a] != ',')
a = a +1;
if(strcmp(&names[y][z], &names[y+1][a]) < 0){
strcpy(temp1,names[y]
strcpy(names[y],names[y+1]);
strcpy(names[y+1],temp1);
}
this was all in 2 for loops, 1 to makes sure all the names get sorted, and the second with variable y, to go thru all the values of names..

anyway i think this was working, if someone sees some errors help me out

for the last name. i just cheat and use same code except it looks like
while(names[y][z] != ',')
z = z + 1;
z = z + 1;
while(names[y][z] != ',')
z = z + 1;
so now i think it should go thru both commas... but it wasn't working so i guess not... i used a compiler with debug mode and it said the program got z up to the value of like 29120 or 21920.. something like that then the program closed with an error message always.

oh, btw if someone comes along with super more efficient way, help out by all means.. i was thinking of breaking up a names[y] into like.. first_name, middle_name, last_name then comparing only first_name and middle_name, or last_name with names[y+1], but i wasn't sure on how i oculd go about doing this.

2. First of all, its going to be a lot easier to manage a single array containing a structure, than it is to manage 3 arrays.

For one thing, in the sort function, you can copy a whole structure, rather than worrying about each member of the structure.

Code:
```typedef struct {
char lname[20];
char fname[20];
char mi;
} person_st;```
A variable length array would be allocated thus
person_st *people = malloc ( n * sizeof(person_st) );

Then you would write 3 near-identical functions to sort by whichever field you want. The great thing is, once you have one working, the other two are dead easy to create.

Code:
```for ( i = n-1 ; i > 0 ; i-- ) {
for ( j = 0 ; j < i ; j++ ) {
if ( strcmp( people[j].lname, people[i].lname) > 0 ) {
person_st temp = people[i];
people[i] = people[j];
people[j] = temp;
}
}
}```

3. but then this way how do i get it all input?
because user have to type in like this
last,first,middle
it sound like this means i store it in what you put as whatever.last
whatever.first, and whatever.middle
but we have to store it in the array i already put which was
names
1 sec, here is what our main looks like. we just have to create all the other functions

#include<stdio.h>
#include<string.h>
int main(void){

int type,num;
char **names;

/* Ask user for how many names*/
printf("How many names?: ");
scanf("%d",&num);

/*Dynamically allocate space for the names*/
names = allocate(num);

/*Get the names for the users*/
getnames(names,num);

/*Ask user how to sort names*/
printf("How to sort names?\n");
printf("1: Last\n2: First\n3: Middle\n");
scanf("%d",&type);

/*Sort the names*/
sort_names(names,num,type);

/*Print the sorted array out*/
print_names(names,num);

return(0);
}

I did all the functions, but i can't figure out sort_names one,
and im not sure what you put i can do?? anyways thanks in advance to someone that post

4. ... I don't suppose you could post what you have of the sort function? Don't quite know how much help you need really.

5. Yes finally figure out what my erros were.. :P

case 2:
for(x = 0; x <= num; x++){
for(y = 1; y <= (num - 1); y++){
/*initialize z and a to zero, so everytime it runs loop, it goes to 0 */
z = 0;
a = 0;
/* this loop cotinues to count, until the previous character is a comma
which means that current character is the letter we wanna compare */
do{
z = z + 1;
}while(names[y][z-1] != ',');
/* this loop same as above one, except it checks the very next string */
do{
a = a + 1;
}while(names[y+1][a-1] != ',');
/* we assign the value of the character after comma, to these temp variables*/
temp1 = names[y][z];
temp2 = names[y+1][a];
/* noW compare the two characters and change them if they needa be changed*/
if(temp1 > temp2){
strcpy(temp, names[y]);
strcpy(names[y],names[y+1]);
strcpy(names[y+1],temp);
}
}
}
break;
}

6. oop post too early i mess up on web page
oh well where i have for loop using y, i have it on y = 1 now
but before i had on y = 0, and i didn't start storing names into y = 0 but i was storing in starting at y = 1, so this was one problem, then i read somewhere that what i was doing wrong, i was using strcmp on a character, so instead i just take it out and compare the characters by use temp1 > temp2 or whatever instead of strcmp... so finally get it.. yay!