Thread: Dissecting a string...

  1. #1

    Unhappy 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

    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){
    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.

    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

    So start with
    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.

    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. #3
    but then this way how do i get it all input?
    because user have to type in like this
    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
    1 sec, here is what our main looks like. we just have to create all the other functions

    int main(void){

    int type,num;
    char **names;

    /* Ask user for how many names*/
    printf("How many names?: ");

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

    /*Get the names for the users*/

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

    /*Sort the names*/

    /*Print the sorted array out*/


    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. #4
    Registered User
    Join Date
    Sep 2001
    ... I don't suppose you could post what you have of the sort function? Don't quite know how much help you need really.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #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 */
    z = z + 1;
    }while(names[y][z-1] != ',');
    /* this loop same as above one, except it checks the very next string */
    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]);

  6. #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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM