Thread: Consolidate multiple strings to a single one

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    8

    Question Consolidate multiple strings to a single one

    I'm fairly new to the C Programming language but am currently taking a course on it. One of the projects is to write a phonebook/contacts list. All of my phonebook works except for listing contacts in alphabetical order by first name while also keeping the last name and phone number associated with the correct first name. I am unsure as to how to accomplish this.

    Is there any way to take multiple strings and consolidate them to a single one where I could then tell the code to list that single string in alphabetical order? (This should work regardless of the number of contacts added to the phonebook. Assume all contacts have unique names and numbers).

    Here is a portion of my code showing only what has to be while omitting everything except what needs to be changed. If more code is needed I can post what is necessary. Any help is appreciated. I am using DevC++ however this is only C Language.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <malloc.h>
    
    //Structure for phonebook list
    typedef struct phonebook 
    {
            char firstname[50];
            char lastname[50];
            char phonenumber[50];
    } list;
    
    list *contacts;
    
    //Function prototypes & global variables
    void addcontact(struct phonebook *contacts, int count);
    void deletecontact(struct phonebook *contacts, int count);
    void listcontacts(struct phonebook *contacts, int count);
    void randomcontact(struct phonebook *contacts, int i);
    void deleteall(struct phonebook *contacts, int count);
    void showsearch(struct phonebook *contacts, int count);
    void sortcontacts(struct phonebook *contacts, int count);
    int searchcontacts(struct phonebook *contacts, int count, char *name);
    struct phonebook *contacts;
    int choice;
    int count = 0; 
    int tally = 0;
    
    //Phonebook menu
    int main()
    {
        contacts = (struct phonebook *)malloc(sizeof(struct phonebook));
        srand(time(NULL));
        
        do
        {
            printf("\n--PHONEBOOK MENU--\n");
            printf("1. Add Contact\n");
            printf("2. Delete Contact\n");
            printf("3. List Contacts\n");
            printf("4. Alphabetical List\n");
            printf("5. Search Contacts\n");
            printf("6. Random Contact\n");
            printf("7. Delete All Contacts\n");
            printf("8. Exit\n");
            printf("To make a selection, enter a number 1-7:\n");
            scanf("%d", &choice);
            
            switch (choice)
            {
                case 1:        //Adds Contact
                    addcontact(contacts, count);
                    count++;
                    break;
                    
                case 2:        //Deletes single contact
                    deletecontact(contacts, count);
                    count--;
                    printf("\n--CONTACT SUCCESSFULLY DELETED--\n");
                    break;
                    
                case 3:        //Lists contacts
                    listcontacts(contacts, count);
                    printf("\n");
                    break;
                
                case 4:        //Lists contacts alphabetically
                    sortcontacts(contacts,count);
                    printf("\n");
                    break;
                    
                case 5:        //Search contacts
                    showsearch(contacts, count);
                    printf("\n");
                    break;
                
                case 6:        //Lists random contact
                    tally = rand() % count;
                    randomcontact(contacts, tally);
                    printf("\n");
                    break;
                    
                case 7:        //Deletes all contacts
                    count = 0;
                    printf("\n--ALL CONTACTS SUCCESSSFULLY DELETED--\n");
                    printf("\n");
                    break;
                    
                case 8:        //Exits phonebook
                    printf("\n--PHONEBOOK EXITED--\n");
                    return 0;
            }//End switch
        }//End do
        
        while (1);
        
        //Return used memory
              free(contacts);
              contacts = NULL;
               
              return 0;
    }//End main
    
    //Function to add contacts
    void addcontact(struct phonebook *contacts, int count)
    {
        printf("\n--ADD NEW CONTACT--");
        printf("\nEnter contacts name and number (First Last Number):\n");
        scanf("%s %s %s", contacts[count].firstname, contacts[count].lastname, contacts[count].phonenumber);
        printf("\n--CONTACT SUCCESSFULLY ADDED--\n");
    }//End addcontact
    
    
    //Function to list contacts alphabetically by first name
    void sortcontacts(struct phonebook *contacts, int count)
    {
        int i, j;
        char tempname[50];
        for (i = 0; i < count; j++)
        {
        if (strcmp(contacts[i].firstname, contacts[j].firstname) > 0)
            {
            strcpy(tempname, contacts[i].firstname);
            strcpy(contacts[i].firstname, contacts[j].firstname);
            strcpy(contacts[j].firstname, tempname);
            printf("\n%s %s %s\n", contacts[j].firstname, contacts[i].lastname, contacts[i].phonenumber);
            }//End if     
        }//End for
    }//End sortcontacts

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Your sort loop is on the right track.
    Code:
    if (strcmp(contacts[i].firstname, contacts[j].firstname) > 0) {
      struct phonebook temp = contacts[i];
      contacts[i] = contacts[j];
      contacts[j] = temp;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    8
    Quote Originally Posted by Salem View Post
    Your sort loop is on the right track.
    Code:
    if (strcmp(contacts[i].firstname, contacts[j].firstname) > 0) {
      struct phonebook temp = contacts[i];
      contacts[i] = contacts[j];
      contacts[j] = temp;
    }

    So I tried swapping what you provided with what I already had and it didn't seem to do anything to help. In fact I couldn't get it to work at all. I'm not entirely sure what it is supposed to do or if I swapped it incorrectly.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Well the first problem would be your broken loop, where you initialise i, but increment the uninitialised j
    > for (i = 0; i < count; j++)

    Before you create a mass of code, make a separate file where you can test just ONE function easily without a complicated UI around it.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    //Structure for phonebook list
    typedef struct phonebook
    {
            char firstname[50];
            char lastname[50];
            char phonenumber[50];
    } phonebook ;
    
    void print ( phonebook *pb, size_t n ) {
      printf("The phone book\n");
      for ( size_t i = 0 ; i < n ; i++ ) {
        printf("%s %s %s\n", pb[i].firstname, pb[i].lastname, pb[i].phonenumber);
      }
    }
    
    void sortcontacts(struct phonebook *contacts, int count)
    {
      int i, j = 0;
      for (i = 0; i < count; i++)
      {
        if (strcmp(contacts[i].firstname, contacts[j].firstname) > 0)
        {
          struct phonebook temp = contacts[i];
          contacts[i] = contacts[j];
          contacts[j] = temp;
        }
      }
    }
    
    int main ( ) {
      phonebook book[4] = {
        { "Fred", "Flintstone", "123" },
        { "Barney", "Rubble", "456" },
        { "Wilma", "Flintstone", "876" },
        { "Betty", "Rubble", "789" },
      };
      print(book,4);
      sortcontacts(book,4);
      print(book,4);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple condition for single variable in single line
    By merafiq in forum C Programming
    Replies: 2
    Last Post: 03-13-2016, 05:26 PM
  2. reading multiple strings from single row....
    By satty in forum C Programming
    Replies: 13
    Last Post: 08-06-2010, 07:39 AM
  3. combining multiple libs into a single lib
    By eburghardt in forum Windows Programming
    Replies: 0
    Last Post: 06-16-2006, 02:51 PM
  4. Single API for multiple CGI
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 10-09-2001, 06:31 AM
  5. changing from multiple-doc to single-doc
    By swordfish in forum C++ Programming
    Replies: 2
    Last Post: 08-31-2001, 07:52 PM

Tags for this Thread