使用strcmp和strcpy按字母顺序对省份名称进行排序

xdnvmnnf  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(97)

我正在尝试实现strcmpstrcpy以按字母顺序重新排列名称,但名称数组初始化出现了问题。
无法如预期在主控台上打印状态数组。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char sort(char [], char []);

int main() {
    char strStates[8] = {
        'Ontario', 'Quebec', 'Manitoba', 'Alberta',
        'British Colombia', 'Nova Scotia', '\0'
    };
    char strSorted[] = { '\0' };
    int x = 0;
    
    printf("\nThe list of states before being sorted in alphabetical order: %s", strStates);
    
    for (x = 0; x < 7; x++) {
        printf("\n%s", strStates);
    }
    
    sort(strStates[x], strSorted[x]);
    
    printf("\nThe list of states sorted alphabetically are: ");
    
    for (x = 0; x < 4; x++) {
        printf("\n%s", strStates[x]);
    }
    return 0;
}
    
char sort(char string1[], char string2[]) {
    int x, y = 0;
    
    for (x = 0; x < 3; x++) {
        for (y = 1; y < 4; y++) {
            if (strcmp(string1[x], string1[y]) > 0) {
                strcpy(string2[y], string1[x]);
                strcpy(string1[x], string1[y]);
                strcpy(string[y], string2[y]);
            }
        }
    }
}
unhi4e5o

unhi4e5o1#

You declared a character array of 8 characters

char strStates[8] = {'Ontario', 'Quebec', 'Manitoba', 'Alberta','British                 Colombia','Nova Scotia','\0'};

and trying to initialize it with multibyte character constant as for example 'Ontario' that have implementation defined values.
It seems you want to declare an array of string literals. In this case you should write for example

const char * strStates[] = 
{
    "Ontario", "Quebec", "Manitoba", "Alberta", "British Colombia","Nova  Scotia" 
};

Also it is a bad idea yo use magic numbers like 8

char strStates[8] = //...

or 7

for (x = 0; x < 7; x++)

or 4

for (x = 0; x < 4; x++)

This makes the code unclear.
You can determine the size of the declared array as shown above the following way

const char * strStates[] = 
{
    "Ontario", "Quebec", "Manitoba", "Alberta", "British Colombia","Nova  Scotia" 
};

const size_t N = sizeof( strStates ) / sizeof( *strStates );

As a result for example the for loop that outputs elements of the array can look like

puts( "The list of states before being sorted in alphabetical order:" );     
for ( size_t i = 0; i < N; i++) {
    puts( strStates[i] );
}

The array strSorted declared like

char strSorted[] = {'\0'};

is not used in your program. Remove the declaration.
This call of the function sort

sort(strStates[x], strSorted[x]);

does not make sense. The argument expressions have the type char while the function expects arguments of the type char * .
The function sort can be declared the following way

void sort( const char *[], size_t );

and called like

sort( strStates, N );

The function definition that implements the bubble sort method can look like

void sort( const char * s[], size_t n ) 
{
    for ( size_t  sorted = 0; !( n < 2 ); n = sorted ) 
    {
        for ( size_t i = sorted = 1; i < n; i++ ) 
        {
            if ( strcmp( s[i], s[i-1] ) < 0 ) 
            {
                const char *tmp = s[i];
                s[i] = s[i-1];
                s[i-1] = tmp;

                sorted = i;
            }
        }
    }
}

Here is a demonstration program.

#include <stdio.h>
#include <string.h>

void sort( const char * s[], size_t n )
{
    for (size_t sorted = 0; !( n < 2 ); n = sorted)
    {
        for (size_t i = sorted = 1; i < n; i++)
        {
            if (strcmp( s[i], s[i - 1] ) < 0)
            {
                const char *tmp = s[i];
                s[i] = s[i - 1];
                s[i - 1] = tmp;

                sorted = i;
            }
        }
    }
}

int main( void )
{
    const char *strStates[] =
    {
        "Ontario", "Quebec", "Manitoba", "Alberta", "British Colombia","Nova  Scotia"
    };

    const size_t N = sizeof( strStates ) / sizeof( *strStates );

    puts( "The list of states before being sorted in alphabetical order:" );

    for (size_t i = 0; i < N; i++) {
        puts( strStates[i] );
    }

    sort( strStates, N );

    puts( "\nThe list of states sorted alphabetically are:" );

    for ( size_t i = 0; i < N; i++) {
        puts( strStates[i] );
    }
}

The program output is

The list of states before being sorted in alphabetical order:
Ontario
Quebec
Manitoba
Alberta
British Colombia
Nova  Scotia

The list of states sorted alphabetically are:
Alberta
British Colombia
Manitoba
Nova  Scotia
Ontario
Quebec
lsmd5eda

lsmd5eda2#

  • "...名称数组初始化出现问题。"*

除了阵列初始化之外,还有其他问题....
注意:C中的单引号表示char。请使用双引号。更改:

'Ontario'

"Ontario"

无处不在。
另外,作为数组中最后一个元素的\0不是必需的。每个...“”字符串都包含自己的空终止符。数组初始化:数组的大小必须包含可见字符的数量加上一个额外的字节作为空终止符-数组中最长的字符串。
变更

char strStates[8] = {'Ontario', ..., 'Nova Scotia','\0'};

至(包括2D数组符号)

char strStates[][18] = {"Ontario", ..., "Nova Scotia"};//British Columbia (17 + 1)
                  ^     ^       "       "           "
                 |longest string in collection

或者改为this(避免需要知道数组中最长的字符串)。

char *strStates[] = {"Ontario", ..., "Nova Scotia"};

对于比较问题,下面是一个简单的示例,说明如何使用strcmp()实现的compare函数和qsort()函数进行比较:

int comp(const void* a, const void* b);

int main(void)
{
    char strStates[][18] = {"Ontario", "Quebec", "Manitoba", "Alberta","British Colombia","Nova Scotia"};

    qsort(strStates, size, 18, comp);

    return 0;
}

int comp(const void* a, const void* b) {
    const char* aa = (const char*)a;
    const char* bb = (const char*)b;
    return strcmp(aa, bb);
}

相关问题