gpt4 book ai didi

c - 使用双指针对字符串中的字母进行排序

转载 作者:行者123 更新时间:2023-11-30 20:32:39 25 4
gpt4 key购买 nike

我一直在对字符串中的字母进行排序。必须使用双指针按字母顺序排序。

#define SIZE 21 //defined index of the array

int _tmain(int argc, _TCHAR* argv[])
{
// an array with 21 strings
char * string[SIZE] = { "dfghgfd", "rtyukljgfds", "sdsf", "fdgdfhg", "fgfhgjghj", "nmjlkjlk", "qwasazx",
"zxdfd", "opiljkg", "vcxdfgfd", "fgfhgfhgh", "bvvh", "bb", "dfsdretr",
"reuio", "cvbmhg", "fgfdyrtyty", "fgdgdfgdfgdf", "g", "fgdfg", "ghghgfhv" };

------------------------访问数组中的每个字符串------------------ ------

    int Anz, i; //Anz - 21 strings

//declared new array
char** new_string;
new_string = (char**)malloc(sizeof(string));
Anz = sizeof(string) / sizeof(char*);

for (i = 0; i < Anz; i++)
{
new_string[i] = (char*)malloc(strlen(string[i]) + 1);
strcpy(new_string[i], string[i]);
}


------------------------ 对字母进行排序------------------------ ----------------

    char* temp;
int k, j;

for (k = 0; k<Anz - 1; k++)
{
for (j = k + 1; j<Anz; j++)
{
if (new_string[k] > new_string[j])
{
temp = new_string[k];
new_string[k] = new_string[j];
new_string[j] = temp;
}
}
}

return 0;
}

最佳答案

不知道为什么需要“a”数组,因为您可以使用新的字符串数组交换字符。另外,使用 char * 来保存值的长度有点奇怪,但我想它是有效的,因为字符串长度非常短。

关于c - 使用双指针对字符串中的字母进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47232241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com