-------------------------- closed ------------ ----------------用双指针排列字符串中的字母
-2
A
回答
0
m = n[i];// its wrong, 'm' is an integer and you are assigning char string.
ň应该INT指针,因为你是存储长度进去。为什么这么多的双指针?你可以简单地做。
检查下面的修改。
#define SIZE 21 //defined index of the array
int main(int argc,char* argv[])
{
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 k;
char *temp;
for (k = 0; k<SIZE - 1; k++)
{
for (j = k + 1; j<SIZE; j++)
{
if (strcmp(string[k],string[j])>0)
{
temp = string[k];
string[k] = string[j];
string[j] = temp;
}
}
}
// for (k = 0; k<SIZE; k++)
// printf("%s\n",string[k]);
return 0;
}
0
不确定为什么需要“a”数组,因为您可以使用新的字符串数组交换字符。此外,使用char *来保存值的长度是奇怪的,但我想它的工作原理,因为字符串的长度很短。
0
不知道您是否想对字母或单词进行排序。评论部分对单词进行排序。
检查malloc的返回值,因为它可能会失败。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 21 //defined index of the array
int main(int argc, char** 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
int width = 0, len = 0;
//declared new array
char** new_string;
Anz = sizeof(string)/sizeof(*string);
if (NULL == (new_string = malloc (Anz * sizeof(*new_string)))) {
fprintf (stderr, "malloc failed\n");
return 0;
}
for (i = 0; i < Anz; i++)
{
len = strlen (string[i]) + 1;
if (len > width) {
width = len;//used later when printing
}
if (NULL == (new_string [i] = malloc (width))) {
fprintf (stderr, "[i] malloc failed\n");
//free memory allocated
while (i) {
i--;
free (new_string[i]);
}
free (new_string);
return 0;
}
strcpy(new_string[i], string[i]);
}
/*
//sort words
int word = 0;
while (word < Anz - 1) {
int end = word;
int temp = end + 1;
while (end >= 0 && 0 > strcmp (new_string[temp], new_string[end])) {
char *hold = new_string[temp];
new_string[temp] = new_string[end];
new_string[end] = hold;
end--;
temp--;
}
word++;
}
word = 0;
while (word < Anz) {
printf ("Anz[%2d] is %s\n", word, new_string[word]);
word++;
}
*/
//sort letters in word
char swap;
int sorted;
int prior;
int each;
int start;
word = 0;
while (word < Anz)
{
start = 0;//new_string[Anz][0]
sorted = start;
prior = start;
each = start + 1;//new_string[Anz][1]
printf ("Anz[%2d] is %-*s", word, width, new_string[word]);
while ('\0' != new_string[word][each]) {
while (prior >= 0 && new_string[word][each] < new_string[word][prior]) {
swap = new_string[word][each];
new_string[word][each] = new_string[word][prior];
new_string[word][prior] = swap;
each--;//move toward start of string
prior--;
}
sorted++;//move toward end of string
prior = sorted;
each = prior + 1;
}
printf (" sorted %s\n", new_string[word]);
word++;
}
//release allocated memory
word = 0;
while (word < Anz) {
free (new_string[word]);
word++;
}
free (new_string);
return 0;
}
相关问题
- 1. 字符串链表使用双指针
- 2. 按字母顺序排列字符串中的字母 - SAS
- 3. 更改字符串中的双字母
- 4. 指向字符串的指针阵列
- 5. scanf字符指针中的字符串
- 6. 双指针字符操作
- 7. Ç指定字符串指针到其他字符串指针
- 8. 使用指向字符串的指针初始化指向字符串中的字符的指针
- 9. 字符串指针
- 10. 使用指针指向字符串中的某些字符
- 11. 按字母顺序排序字符串和Python中的字符串列表
- 12. Groovy中用字母和数字排序的数字字符串
- 13. C - fscanf可以使用字符指针,但不能使用双字符指针?
- 14. C指针铸造:单个int指针双字符指针
- 15. 如何重新排列字符串中的字母?
- 16. 按字母顺序排列字符串中的单词
- 17. 阵列中的字母排序字符串
- 18. 复制字符串指针,指针
- 19. 如何在C++中使用字符串和字符串指针
- 20. Javascript字符串搜索,双字母出现在字母i
- 21. C#排序Arraylist字符串按字母顺序排列
- 22. 字符指针和字符串
- 23. 字符串与字符指针
- 24. 字符指针为字符串
- 25. 字符指针转换为字符串
- 26. 混合字符串值的字母数字排序:字母数字字母
- 27. Wrtiting字符串内存流 - 指向字符串的指针
- 28. 使用C中的字符指针的字符串
- 29. 双指针指向字符串采取导致段故障
- 30. C字符串和指针
有问题吗? – Suren
“*字母*”?你(尝试)分类的话,不是吗? – alk