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:
- Find items that starts with a specified letter,
- 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
strncmp(ptrSehir, "B", 1).qsort()?