用双指针排列字符串中的字母
答
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;
}
答
不确定为什么需要“a”数组,因为您可以使用新的字符串数组交换字符。此外,使用char *来保存值的长度是奇怪的,但我想它的工作原理,因为字符串的长度很短。
答
不知道您是否想对字母或单词进行排序。评论部分对单词进行排序。
检查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;
}
有问题吗? – Suren
“*字母*”?你(尝试)分类的话,不是吗? – alk