使用字符串操作和函数对数组进行冒泡排序
Bubblesorting an array with String Manipulation and Functions
问题
所以我一直在尝试编写一个 2 阶段程序,该程序具有一个字符串(它是一个常量字符串。已给出。)并且:
- 查找以指定字母开头的项目,
- 按字母顺序对该字符串中的项目进行排序。
问题
我的程序的第二部分需要帮助,因为第一部分运行良好。当我执行我的代码时,我应该看到 按字母顺序排序 的城市。但相反,我看到了 this 胡说八道。
我是一个彻头彻尾的菜鸟,我在 string/character 操作相关主题的每一点上都遇到了问题。因此,我们将不胜感激。
这是我的代码:
#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]);
}
}
对不起,初始化变量不好,而且还用非英语写东西,但这段代码本来是我的工作表,英语不是我的母语。和平
strok
returns 原始字符串中的一个指针。当您对 cities 数组进行排序时,您会通过重写不同大小的单词来弄乱原始字符串。
您需要做的是将每个城市名称复制到一个 char[]
足够大的文件中,这样您就可以在上面重写任何其他城市名称而不会弄乱其他城市名称。
这是一个应该有效的例子:
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]);
}
}
旁注:您应该将循环中的常量 6
、7
替换为更通用的内容,以便相同的函数适用于其他城市数。
问题
所以我一直在尝试编写一个 2 阶段程序,该程序具有一个字符串(它是一个常量字符串。已给出。)并且:
- 查找以指定字母开头的项目,
- 按字母顺序对该字符串中的项目进行排序。
问题
我的程序的第二部分需要帮助,因为第一部分运行良好。当我执行我的代码时,我应该看到 按字母顺序排序 的城市。但相反,我看到了 this 胡说八道。
我是一个彻头彻尾的菜鸟,我在 string/character 操作相关主题的每一点上都遇到了问题。因此,我们将不胜感激。
这是我的代码:
#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]);
}
}
对不起,初始化变量不好,而且还用非英语写东西,但这段代码本来是我的工作表,英语不是我的母语。和平
strok
returns 原始字符串中的一个指针。当您对 cities 数组进行排序时,您会通过重写不同大小的单词来弄乱原始字符串。
您需要做的是将每个城市名称复制到一个 char[]
足够大的文件中,这样您就可以在上面重写任何其他城市名称而不会弄乱其他城市名称。
这是一个应该有效的例子:
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]);
}
}
旁注:您应该将循环中的常量 6
、7
替换为更通用的内容,以便相同的函数适用于其他城市数。