Search code examples
carraysstringsortingbubble-sort

Bubblesorting an array with String Manipulation and Functions


Question

So i've been trying to code a 2-stage program that has a string(It's a constant string. It's given.) and does:

  1. Find items that starts with a specified letter,
  2. Sort items in that string alphabetically.

Problem

I need help with the second part of my program, because first part is doing fine. I should see the cities alphabetically sorted when I execute my code. But instead I see this nonsense.

I'm a complete noob and I'm having problems at every bit of string/character manipulation related subject. So any help about anything will be appreciated.


Here is my code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void sortStartwB(const char strSehir[]);
void sortCities(const char strSehir[]);

int main()
{
    const char strSehir[100] = "Bursa Yozgat Canakkale Balikesir Adana Eskisehir Bilecik";
    sortStartwB(strSehir);
    sortCities(strSehir);
    return 0;
}

void sortStartwB(const char strSehir[])
{
    char strNew[100];
    strcpy(strNew, strSehir);
    char *ptrSehir;
    char strBsehir[50] = {}; 

    ptrSehir = strtok(strNew, " ");


    while (ptrSehir != NULL)
    {
        if (strncmp(ptrSehir, "B", 1) == 0)
        {
            strcat(strBsehir, ptrSehir);
            strcat(strBsehir, " ");
        }
        ptrSehir = strtok(NULL, " ");
    }

    printf("\nb ile baslayan sehirler : \n%s\n", strBsehir);
}

void sortCities(const char strSehir[])
{
    char strFunc[100];
    char *strSorted[100];
    int i = 0;

    char temp[50];

    strcpy(strFunc, strSehir);

    strSorted[i]=strtok(strFunc, " ");

    while (strSorted[i] != NULL)
    {
        strSorted[++i] = strtok(NULL, " ");
    }

    for (int j=0; j<6; j++)
    {
        for (int k=j+1; k<7; k++)
        {
            if (strcmp(strSorted[j], strSorted[k]) > 0)
            {
                strcpy(temp, strSorted[j]);
                strcpy(strSorted[j], strSorted[k]);
                strcpy(strSorted[k], temp);
            }
        }
    }

    for (int x=0; x < 7; x++)
    {
        printf("\n%s", strSorted[x]);
    }
}

Sorry for the initializing variables bad and also writing stuff in non-English but this piece of code meant to be my worksheet and English is not my native. Peace


Solution

  • The strok returns a pointer in the original string. When you sort the cities array, you're messing up the original string by rewriting into it words of differents sizes.

    What you have to do is copying each city name in a char[] large enough so you can rewrite any other city name on it without messing up other city names.

    Here is an example of something that should works :

    void sortCities(const char strSehir[])
    {
        char strFunc[100];
        char strSorted[100][100];
        char *city;
        int i = 0;
    
        char temp[50];
    
        strcpy(strFunc, strSehir);
    
        for (city = strtok(strFunc, " "); city != NULL; city = strtok(NULL, " "))
            strcpy(strSorted[i++], city);
    
        for (int j=0; j<6; j++)
        {
            for (int k=j+1; k<7; k++)
            {
                if (strcmp(strSorted[j], strSorted[k]) > 0)
                {
                    strcpy(temp, strSorted[j]);
                    strcpy(strSorted[j], strSorted[k]);
                    strcpy(strSorted[k], temp);
                }
            }
        }
    
        for (int x=0; x < 7; x++)
        {
            printf("\n%s", strSorted[x]);
        }
    }
    

    Aside note : you should replace constants 6, 7 in the loops with something more generic so the same function will work with other numbers of cities.