1

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

5
  • 2
    Avoid adding information that is not relevant to your question, read How to Ask Commented May 28, 2018 at 1:44
  • Please explain this strncmp(ptrSehir, "B", 1). Commented May 28, 2018 at 5:30
  • Compares 2 strings by n characters. strncmp(ptrSehir, "B", 1) looks if the ptrSehir's first character is "B". Commented May 28, 2018 at 10:59
  • Is there a good reason you're not using qsort()? Commented May 28, 2018 at 11:01
  • Only because this is a final exam preparation worksheet and i didn't learn that in our lessons. I'll look up for it, thank you. Commented May 28, 2018 at 11:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay i added numOfCities variable to the program and incremented it in the tokenizing for loop and I think that should do the trick. Thank you for enligthening me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.