Thread: Sorting chars in alphabetical order

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    18

    Sorting chars in alphabetical order

    Ok so part of my program is in swedish sorry for that. My program basically inputs name of car owner, and car brand. And stores that data in a list of max 10 posts.

    Im trying to add a function that will sort out the inputs in alphabetical order, by car brand. The fuction that im talking about is in bold. Right now it is written to sort out in alphabetical by name of the car owner. It dont matter, once get it to work i could just change it to car brand. All help is appreciated.

    The code runs well for me. Menu option 1 adds the name of the car owner and car brand. Menu option 3 is supposed to sorts them by alphabetical order. That is the code that i need to work on.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    /* Data av typen fordon */
    typedef struct fordon {
        char namn[15];                                  /* name of the owner */
        char marke[20];                                      /* car brand */
        char fordonstyp[15];
        char registeringsnummer[10];
    } fordon;
      fordon f[10];
    
    /* Data av typen fordon */
    typedef struct person {
        char id[40];
        int alder;
    } person;
      person p;
    
    
    /* Funktion lägg till fordon */  /* function to add a vehicle */
    bool add(fordon* f) {
      fordon tmp;
      printf("Enter name of owner: ");
      scanf("%s", tmp.namn);                                  /* name of the owner */
      printf("Enter car brand: ");
      scanf("%s", tmp.marke);                               /* car brand */
      /*printf("fordonstyp: ");
      scanf("%s", tmp.fordonstyp);
      printf("registeringsnummer: ");
      scanf("%s", tmp.registeringsnummer);*/
      /* Läs in allt data och lägg i tmp */
    
    
    
      *f = tmp;
      return true; // Allt gick bra
    }
    /* Funktion ta bort fordon */                             /* function to delete a vehicle */
    bool delete(fordon* f){
    fordon tmp;
    int i = 0;
    
    memset(&tmp.namn[0], 0, sizeof(tmp.namn));
    memset(&tmp.marke[0], 0, sizeof(tmp.marke));
    memset(&tmp.fordonstyp[0], 0, sizeof(tmp.fordonstyp));
    memset(&tmp.registeringsnummer[0], 0, sizeof(tmp.registeringsnummer));
    
    *f = tmp;
    return true;
    }
    /* Sort after car brand*/
    int sortera(){
    int c,d;
    char namnb[20], markeb[20], fordonstypb[15],registeringsnummerb[10] ; 
    for (c = 0 ; c < ( 10 ); c++)
      {
        for (d = 0 ; d < 10 - c ; d++)
        {
          if (f[d].namn[0] > f[d+1].namn[0]) 
          {
            strcpy(namnb, f[d].namn);
            strcpy(f[d].namn, f[d+1].namn);
            strcpy(f[d+1].namn, namnb);
          }
        }
    }
    }
    
    int main() {
    
    
    
    
    /* switch meny */                          
    int val = 0, i = 0;
    while(val != 6){
    
      printf("\n1. Add a vehicle");
      printf("\n2. Remove a vehicle");
      printf("\n3. Sort by car brand");                                 /* more like sort by name of owner right now */
      printf("\n4. Print out iinformation about a veichle");
      printf("\n5. Print out vehicle directory");
      printf("\n6. End\n");
      printf("Enter a choice (1-6) ");
    
    scanf("%d", &val);
    
    switch(val){
    case 1:
    i = 0;
    while(  f[i].namn[0] != '\0' && i < 10){
    i++;}
    printf("%d", i);
    if(f[9].namn[0] != '\0'){
    printf("\n Registret är fullt\n");
    break;
    
    
    }
    else{
     add(&f[i]);
    break;}
    break;
    
    case 2:
    i= 0;
    printf("\nSkriv in platsen(1-10) på det fordon du vill ta  bort: ");
    scanf("%d", &i);
    delete(&f[i-1]);
    break;
    
    case 3:
    sortera();
    break;
    
    case 4:
    i = 0;
    printf("\nSkriv in platsen(1-10) på det fordon du vill skriva ut information om: ");
    scanf("%d", &i);
    if(i < 1 || i >10){
    ("\nFel! Välj någon av platserna 1-10.\n");}
    
    else if (f[i-1].namn[0] == '\0'){
    printf("\nPlatsen är tom.\n");}
    else {
    printf("%d.%s    %s    %s    %s\n", i, f[i-1].namn, f[i-1].marke,  f[i-1].fordonstyp, f[i-1].registeringsnummer);}
    break;
    
    case 5:
    for(i=0; i<10; i++){
    printf("%d.%s    %s    %s    %s\n", i + 1, f[i].namn, f[i].marke,  f[i].fordonstyp, f[i].registeringsnummer)  ;}
    break;
    
    case 6:
    break;
    
    default:
    printf("\nFel! Välj något av alternativen 1-6.\n");
    }
    }
    
    return 0;
    
    }
    Last edited by Heisenberg800; 11-24-2017 at 12:00 PM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I'd think you'd be better off after getting your input then sort the array it'd be faster and just a one time thing. so yeah faster in that respect too.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct mySorter
    {
    int a;
    char strings[20]; 
    }sort_strct;
    
    
    int main(void)
    {
     
     const char *addNames[5] = { "Zorta" , "Farlain" , "Alpha", "Delta", "Wagon" };
     
     sort_strct  sortme[5];
     
     for (int g = 0; g < 5; g++)
        printf("%s\n", addNames[g]);
     
    
     int d = 0;
     for ( ; d < 5; d++)
     {    
         strcpy(sortme[d].strings, addNames[d]);
         sortme[d].a = d;
     }
     
     for (d = 0; d<5;d++)
     {
         printf("num %d: name %s:\n", sortme[d].a, sortme[d].strings);
     }
      sort_strct temp;
     
      for (int i = 0; i < 5; i++) 
      {
          for (int j = i+1; j < 5; j++)
          {
                if (strcmp(sortme[i].strings, sortme[j].strings) > 0)
                {
                    temp = sortme[i]; 
                    sortme[i] = sortme[j]; 
                    sortme[j] = temp; 
                }
            }
        }
    
     printf("\n");
         for (int d = 0; d < 5; d++)
         {
             printf("num %d: name %s:\n", sortme[d].a, sortme[d].strings);
         }
     
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by userxbw View Post
    text
    Thanks userxbw! Your help is much appreciated. I implemented your code to my program. Thanks for introducing "strcmp". That's new to me and i will look more in to that.
    However it still don't work quite right. The function do sort by car brand in alphabetical order. But the first posts will be empty (pictures below). I will try to solve this on my own but any input to why this is happening is appreciated.

    I noticed that I solved the problem by changing
    Code:
    "if (strcmp(f.marke, f[j].marke) > 0)"
    to
    Code:
    "if (strcmp(f.marke, f[j].marke) < 0)"
    But then it sorted by car brand in descending order. And thats not what i want.


    Before sorting by car brand:

    Sorting chars in alphabetical order-1-png
    After sorting by car brand:
    Sorting chars in alphabetical order-2-png

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    /* Data av typen fordon */
    typedef struct fordon {
        char namn[15];                                  /* name of the owner */
        char marke[20];                                      /* car brand */
        char fordonstyp[15];
        char registeringsnummer[10];
    } fordon;
      fordon f[10];
      
    
    /* Data av typen fordon */
    typedef struct person {
        char id[40];
        int alder;
    } person;
      person p;
    
    
    /* Funktion lägg till fordon */
    bool add(fordon* f) {
      fordon tmp;
      printf("Enter name of owner: ");
      scanf("%s", tmp.namn);                                  /* name of the owner */
      printf("Enter car brand: ");
      scanf("%s", tmp.marke);                               /* car brand */
      /*printf("fordonstyp: ");
      scanf("%s", tmp.fordonstyp);
      printf("registeringsnummer: ");
      scanf("%s", tmp.registeringsnummer);*/
      /* Läs in allt data och lägg i tmp */
    
    
    
      *f = tmp;
      return true; // Allt gick bra
    }
    /* Funktion ta bort fordon */
    bool delete(fordon* f){
    fordon tmp;
    int i = 0;
    
    memset(&tmp.namn[0], 0, sizeof(tmp.namn));
    memset(&tmp.marke[0], 0, sizeof(tmp.marke));
    memset(&tmp.fordonstyp[0], 0, sizeof(tmp.fordonstyp));
    memset(&tmp.registeringsnummer[0], 0, sizeof(tmp.registeringsnummer));
    
    *f = tmp;
    return true;
    }
    /* Sort after car brand*/
    int sortera(){
      int i,j;
    
      fordon temp;
    
    for (int i = 0; i < 10; i++) 
      {
          for (int j = i+1; j < 10; j++)
          {
                if (strcmp(f[i].marke, f[j].marke) > 0)
                {
                    temp = f[i]; 
                    f[i] = f[j]; 
                    f[j] = temp; 
                }
            }
        }
    }
    
    int main() {
    
    
    
    
    /* switch meny */
    int val = 0, i = 0;
    while(val != 6){
    
      printf("\n1. Add a vehicle");
      printf("\n2. Remove a vehicle");
      printf("\n3. Sort by car brand");
      printf("\n4. Print out iinformation about a veichle");
      printf("\n5. Print out vehicle directory");
      printf("\n6. End\n");
      printf("Enter a choice (1-6) ");
    
    scanf("%d", &val);
    
    switch(val){
    case 1:
    i = 0;
    while(  f[i].namn[0] != '\0' && i < 10){
    i++;}
    printf("(%d)", i+1);
    if(f[9].namn[0] != '\0'){
    printf("\n Registret är fullt\n");
    break;
    
    
    }
    else{
     add(&f[i]);
    break;}
    break;
    
    case 2:
    i= 0;
    printf("\nSkriv in platsen(1-10) på det fordon du vill ta  bort: ");
    scanf("%d", &i);
    delete(&f[i-1]);
    break;
    
    case 3:
    sortera();
    break;
    
    case 4:
    i = 0;
    printf("\nSkriv in platsen(1-10) på det fordon du vill skriva ut information om: ");
    scanf("%d", &i);
    if(i < 1 || i >10){
    ("\nFel! Välj någon av platserna 1-10.\n");}
    
    else if (f[i-1].namn[0] == '\0'){
    printf("\nPlatsen är tom.\n");}
    else {
    printf("%d.%s    %s    %s    %s\n", i, f[i-1].namn, f[i-1].marke,  f[i-1].fordonstyp, f[i-1].registeringsnummer);}
    break;
    
    case 5:
    for(i=0; i<10; i++){
    printf("%d.%s    %s    %s    %s\n", i + 1, f[i].namn, f[i].marke,  f[i].fordonstyp, f[i].registeringsnummer)  ;}
    break;
    
    case 6:
    break;
    
    default:
    printf("\nFel! Välj något av alternativen 1-6.\n");
    }
    }
    
    return 0;
    
    }
    Last edited by Heisenberg800; 11-25-2017 at 10:56 AM.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Delete this post

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by userxbw View Post
    text
    I solved the problem by changing the if statement to this
    Code:
    (strcmp(f[i].marke, f[j].marke) > 0 && f[j].marke[0] != '\0')
    The empty posts seemed to create the problem. So i made sure to not if statement if the post was empty.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Heisenberg800 View Post
    I solved the problem by changing the if statement to this
    Code:
    (strcmp(f[i].marke, f[j].marke) > 0 && f[j].marke[0] != '\0')
    The empty posts seemed to create the problem. So i made sure to not if statement if the post was empty.
    yeah if you're talking about the extra print outs even though it is not full, you might be able to use size of array then it will only go as far as the amount inside of it as well. that too should work. maybe not... let me try your way.

    yep
    you're smarter then me
    but you should still take a look at its return
    Code:
    /* Sort after car brand*/
    //int sortera(){ // does it really need a return value??? 
    // if yes then it is missing one. 
    void sortera(){
        // they are declared inside your for loops. 
     // int i,j; 
     
      fordon temp;
     
    for (int i = 0; i < 10; i++) 
      {
          for (int j = i+1; j < 10; j++)
          {
               // if (strcmp(f[i].marke, f[j].marke) > 0)
                if (strcmp(f[i].marke, f[j].marke) > 0 && f[j].marke[0] != '\0')
                {
                    temp = f[i]; 
                    f[i] = f[j]; 
                    f[j] = temp; 
                }
            }
        }
    }
    Last edited by userxbw; 11-25-2017 at 02:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting strings into alphabetical order
    By dude_tron_1982 in forum C Programming
    Replies: 5
    Last Post: 12-10-2010, 12:34 PM
  2. names in alphabetical order?
    By n3cr0_l0rd in forum C Programming
    Replies: 21
    Last Post: 02-06-2009, 08:59 PM
  3. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM
  4. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM
  5. Alphabetical order
    By BubbleMan in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2001, 03:38 PM

Tags for this Thread