我如何在C中使用一系列指针?

发布于 2025-02-07 05:26:32 字数 1362 浏览 2 评论 0原文

基本上,我有一系列的字符串,我需要检查它们是否按照词法顺序排列(对上或较低的情况不敏感,这意味着它是哪一个无关紧要)。为此,我采用了已经创建的strCMP功能并重新安排了它,以使其上层或较低的情况都没有关系。但是,当输入str_compare时,它不会输入WARE循环,而只是退出功能。 有人知道为什么吗? 这是代码:

int main(){
...
char * banned_words[N];
...
if (!are_sorted(banned_words, n)) {
        printf("Words are not sorted correctly!\n");
        free_strings(banned_words, n);
        return ERROR;
    }
...
}

bool are_sorted(char * strings[], int n) {

    int length = n;
    int result = 0;

    for (int i = 0; i < length - 1; i++) {
        result += str_compare(strings[i], strings[i + 1]);
    }
        if (result > 0)
            return false;
        else
            return true;

}

int str_compare(char **str1, char **str2){
    while (str1 && str2) {
        if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){
            **str1++;
            **str2++;}
        else if (str2 >= 'A' && str2<='Z' && (str2 + 'a' - 'A') == str1){
            **str1++;
            **str2++;}
        else if (str1 == *str2){
            **str1++;
            **str2++;}
        else break;
    }

    if (str1 >= 'A' && str1<='Z')
        return (str1 +'a' - 'A') - str2;
    else if (str2 >= 'A' && str2<='Z')
        return str1 - (str2 +'a'-'A');
    else return str1 - str2;
}

Basically i have an array of strings and i need to check if they are arranged in lexographic order (case insensitive of upper or lower case, meaning it doesn't matter which one it is). For that I took the already created strcmp function and re-arranged it so that it won't matter if it's upper or lower case. But when entering str_compare, it won't enter the while loop and just exits the function.
Does anyone know why?
Here is the code:

int main(){
...
char * banned_words[N];
...
if (!are_sorted(banned_words, n)) {
        printf("Words are not sorted correctly!\n");
        free_strings(banned_words, n);
        return ERROR;
    }
...
}

bool are_sorted(char * strings[], int n) {

    int length = n;
    int result = 0;

    for (int i = 0; i < length - 1; i++) {
        result += str_compare(strings[i], strings[i + 1]);
    }
        if (result > 0)
            return false;
        else
            return true;

}

int str_compare(char **str1, char **str2){
    while (str1 && str2) {
        if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){
            **str1++;
            **str2++;}
        else if (str2 >= 'A' && str2<='Z' && (str2 + 'a' - 'A') == str1){
            **str1++;
            **str2++;}
        else if (str1 == *str2){
            **str1++;
            **str2++;}
        else break;
    }

    if (str1 >= 'A' && str1<='Z')
        return (str1 +'a' - 'A') - str2;
    else if (str2 >= 'A' && str2<='Z')
        return str1 - (str2 +'a'-'A');
    else return str1 - str2;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人爱我别走 2025-02-14 05:26:32

您可以使用类型char *的表达式调用函数str_compare

result += str_compare(strings[i], strings[i + 1]);

而函数参数具有类型char **

int str_compare(char **str1, char **str2){

这是循环

while (str1 && str2) 

调用不确定的行为,因为循环的条件将至少一个指针变成零指针时,循环的状况会评估为false。

同样在if语句中的功能条件下,例如

if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){

else if (str1 == *str2){

没有意义。比较操作数具有不同的类型。

此外,此代码段中的返回语句

if (str1 >= 'A' && str1<='Z')
    return (str1 +'a' - 'A') - str2;
else if (str2 >= 'A' && str2<='Z')
    return str1 - (str2 +'a'-'A');
else return str1 - str2;

也可以产生不可预测的结果。

而且该函数中的代码片段are_sorted

for (int i = 0; i < length - 1; i++) {
    result += str_compare(strings[i], strings[i + 1]);
}
    if (result > 0)
        return false;
    else
        return true;

实际上并不确定是否对字符串的数组进行排序。例如,一对字符串的比较可以产生-1,而另一对字符串的比较可以产生1,结果将等于0 <0 < /code>虽然数组未排序。

函数str_compare可以按照以下方式定义以下方式。

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

int str_compare( const char *s1, const char *s2 )
{
    char c1 = '\0', c2 = '\0';
    while ( *s1 && ( c1 = tolower( ( unsigned char )*s1 ) ) == ( c2 = tolower( ( unsigned char) *s2 ) ) )
    {
        ++s1;
        ++s2;
    }

    return islower( ( unsigned char )*s1 ) && islower( ( unsigned char ) *s2 ) 
        ?  c1 - c2 
        : *s1 - *s2;
}    

int main(void) 
{
    const char *s1 = "HeLlO";
    const char *s2 = "hElLo";

    printf( "\"%s\" == \"%s\" is %d", s1, s2, str_compare( s1, s2 ) );

    return 0;
}

程序输出是

"HeLlO" == "hElLo" is 0

如果您不使用标准C函数TOLOWER,那么函数str_compare可以以下方式查看

int str_compare( const char *s1, const char *s2 )
{
    char c1 = '\0', c2 = '\0';
    while ( 1 )
    {
        c1 = *s1; c2 = *s2;

        if (  'A' <= c1 && c1 <= 'Z' ) c1 += 'a' - 'A';
        if (  'A' <= c2 && c2 <= 'Z' ) c2 += 'a' - 'A';

        if ( c1 != c2 || c1 == '\0' ) break;

        ++s1; ++s2;
    } 

    return 'a' <= c1 && c1 <= 'z' && 'a' <= c2 && c2 <= 'z' 
        ? c1 - c2 : *s1 - *s2;
}

函数aws_sorted然后可以定义以下方式

bool are_sorted( char * strings[], size_t n ) 
{
    size_t i = n;

    if ( n != 0 )
    { 
        ++i;
        while ( i != n && str_compare( strings[i-1], strings[i] ) <= 0 ) ++i;
    }  

    return i == n ;
}

You call the function str_compare with expressions of the type char *

result += str_compare(strings[i], strings[i + 1]);

While function parameters have the type char **.

int str_compare(char **str1, char **str2){

This while loop

while (str1 && str2) 

invokes undefined behavior because the condition of the loop evaluates to false when at least one pointer will become a null pointer.

Also within the function conditions in the if statements like for example

if (str1 >= 'A' && str1 <='Z' && (str1 + 'a' - 'A') == str2){

or

else if (str1 == *str2){

do not make a sense. Compared operands have different types.

Also the return statements in this code snippet

if (str1 >= 'A' && str1<='Z')
    return (str1 +'a' - 'A') - str2;
else if (str2 >= 'A' && str2<='Z')
    return str1 - (str2 +'a'-'A');
else return str1 - str2;

can produce unpredictable results.

And this code snippet within the function are_sorted

for (int i = 0; i < length - 1; i++) {
    result += str_compare(strings[i], strings[i + 1]);
}
    if (result > 0)
        return false;
    else
        return true;

actually does not determine whether the array of strings is sorted. For example the comparison of one pair of strings can yield -1 while the comparison of other pair of strings can yield 1 and the result will be equal to 0 though the array is not sorted.

The function str_compare can be defined the following way as shown in the demonstrative program below.

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

int str_compare( const char *s1, const char *s2 )
{
    char c1 = '\0', c2 = '\0';
    while ( *s1 && ( c1 = tolower( ( unsigned char )*s1 ) ) == ( c2 = tolower( ( unsigned char) *s2 ) ) )
    {
        ++s1;
        ++s2;
    }

    return islower( ( unsigned char )*s1 ) && islower( ( unsigned char ) *s2 ) 
        ?  c1 - c2 
        : *s1 - *s2;
}    

int main(void) 
{
    const char *s1 = "HeLlO";
    const char *s2 = "hElLo";

    printf( "\"%s\" == \"%s\" is %d", s1, s2, str_compare( s1, s2 ) );

    return 0;
}

The program output is

"HeLlO" == "hElLo" is 0

If you may not use the standard C function tolower then the function str_compare can look the following way

int str_compare( const char *s1, const char *s2 )
{
    char c1 = '\0', c2 = '\0';
    while ( 1 )
    {
        c1 = *s1; c2 = *s2;

        if (  'A' <= c1 && c1 <= 'Z' ) c1 += 'a' - 'A';
        if (  'A' <= c2 && c2 <= 'Z' ) c2 += 'a' - 'A';

        if ( c1 != c2 || c1 == '\0' ) break;

        ++s1; ++s2;
    } 

    return 'a' <= c1 && c1 <= 'z' && 'a' <= c2 && c2 <= 'z' 
        ? c1 - c2 : *s1 - *s2;
}

As for the function are_sorted then it can be defined for example the following way

bool are_sorted( char * strings[], size_t n ) 
{
    size_t i = n;

    if ( n != 0 )
    { 
        ++i;
        while ( i != n && str_compare( strings[i-1], strings[i] ) <= 0 ) ++i;
    }  

    return i == n ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文