在字符串中找到最小和最长的单词

发布于 2025-02-06 15:50:24 字数 1368 浏览 1 评论 0原文

我使用了:

保留用于记录单词的开始在哪里。

position_maxposition_min用于记录最终字符单词的位置。

Maxmin用于比较每个单词并找到最长和最小的长度。

lenght是整个字符串的长度。

#include<limits.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<malloc.h>


int main(void)
{
char *stringa = "alex keep all money";
size_t x;
int keep;       
int min;            
int max;        
int position_max;   
int position_min;   
int lenght;     
keep = 1;
max = 0;
min = INT_MAX;
position_max = 0;
lenght = strlen(stringa);
while(x != lenght + 1)
{
    if(isalnum(stringa[x]) != 0)
    {
        keep += 1;
    }
    
    if(isspace(stringa[x]) != 0 || stringa[x] == '\0')
    {
        if(keep > max)
        {
            position_max = x;
            max = keep;
        }
        if(keep < min)
        {
            position_min = x;
            min = keep;
        }
        keep = 0;
    }
    x++;
}
puts("the longest word:");
for(x = position_max - max; x != position_max; x++)
{
    printf("%c",stringa[x]);
}
puts("\n\nthe smallest word:");
for(x = position_min - min; x != position_min; x++)
{
    printf("%c", stringa[x]);
}
return 0;
}

I used:

keep for recording where is the begin of the word.

position_max and position_min for recording position of final char's word.

max and min for compare every word and find the length of longest and the smallest.

lenght is the length of entire string.

#include<limits.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<malloc.h>


int main(void)
{
char *stringa = "alex keep all money";
size_t x;
int keep;       
int min;            
int max;        
int position_max;   
int position_min;   
int lenght;     
keep = 1;
max = 0;
min = INT_MAX;
position_max = 0;
lenght = strlen(stringa);
while(x != lenght + 1)
{
    if(isalnum(stringa[x]) != 0)
    {
        keep += 1;
    }
    
    if(isspace(stringa[x]) != 0 || stringa[x] == '\0')
    {
        if(keep > max)
        {
            position_max = x;
            max = keep;
        }
        if(keep < min)
        {
            position_min = x;
            min = keep;
        }
        keep = 0;
    }
    x++;
}
puts("the longest word:");
for(x = position_max - max; x != position_max; x++)
{
    printf("%c",stringa[x]);
}
puts("\n\nthe smallest word:");
for(x = position_min - min; x != position_min; x++)
{
    printf("%c", stringa[x]);
}
return 0;
}

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

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

发布评论

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

评论(2

七堇年 2025-02-13 15:50:24

正如其他人已经提到的那样 - 您有非初始化变量的问题(xposition_min),也错过了几个边缘案例:当您在开始/结束时有空格时连续的,这是固定版本:

int main(void) {
    const char * const stringa = " alex keep   all money ";
    size_t x = 0; // keep declaration and definition in one place, so it is easy to spot mistakes
    unsigned int keep = 0;
    unsigned int min = 0; // initial min/max should be zero to work properly later during printing
    unsigned int max = 0;
    size_t position_max = 0;
    size_t position_min = 0;
    const size_t length = strlen(stringa);
    
    while (x <= length) {
        if (x == length || isspace(stringa[x])) { // first cover end of string case
            if (keep > 0) { // multiple spaces
                if (max == 0 || keep > max) {
                    position_max = x;
                    max = keep;
                }
                if (min == 0 || keep < min) {
                    position_min = x;
                    min = keep;
                }
                keep = 0;
            }
        } else if (isalnum(stringa[x]))
            keep++;
        x++;
    }

    puts("the longest word:");
    for(x = position_max - max; x < position_max; x++) printf("%c", stringa[x]);
    puts("\n\nthe smallest word:");
    for(x = position_min - min; x < position_min; x++) printf("%c", stringa[x]);
    return 0;
}

as others already mentioned - you have problems with uninitialized variables (x, position_min), also there are couple edge cases missed: when you have spaces at the start/end or multiple consecutive ones, here is fixed version:

int main(void) {
    const char * const stringa = " alex keep   all money ";
    size_t x = 0; // keep declaration and definition in one place, so it is easy to spot mistakes
    unsigned int keep = 0;
    unsigned int min = 0; // initial min/max should be zero to work properly later during printing
    unsigned int max = 0;
    size_t position_max = 0;
    size_t position_min = 0;
    const size_t length = strlen(stringa);
    
    while (x <= length) {
        if (x == length || isspace(stringa[x])) { // first cover end of string case
            if (keep > 0) { // multiple spaces
                if (max == 0 || keep > max) {
                    position_max = x;
                    max = keep;
                }
                if (min == 0 || keep < min) {
                    position_min = x;
                    min = keep;
                }
                keep = 0;
            }
        } else if (isalnum(stringa[x]))
            keep++;
        x++;
    }

    puts("the longest word:");
    for(x = position_max - max; x < position_max; x++) printf("%c", stringa[x]);
    puts("\n\nthe smallest word:");
    for(x = position_min - min; x < position_min; x++) printf("%c", stringa[x]);
    return 0;
}
千柳 2025-02-13 15:50:24

该程序至少是因为在此中使用的变量X

while(x != lenght + 1)

并未初始化的

size_t x;

另一个问题是变量的初始化keep> keep by 1

keep = 1;

此可以结果可以结果对于像“ a a a a”这样的字符串,具有最大长度的子字符串将为“ a” a“,而不是“ bb”提供了最初的变量x 是由0初始化的,或者字符串从Space Cargue ''喜欢“ A” a“ a“),则最大substring将为> “”并使用表达式x = position_max -max在此循环中

for(x = position_max - max; x != position_max; x++)

再次导致不确定的行为。

另外,当字符串包含相邻的白色空间时,您必须考虑情况。

我将编写一个单独的函数,例如,如下演示程序所示。

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

struct SubString
{
    char *position;
    size_t length;
};

struct MinMax
{
    struct SubString min;
    struct SubString max;
};

struct MinMax minmax_string( const char *s, const char *delim )
{
    struct MinMax minmax = 
    { 
        .min = { .position = NULL, .length = 00 }, 
        .max = { .position = NULL, .length = 0 } 
    };

    for ( s += strspn( s, delim ); *s; s += strspn( s, delim )  )
    {
        const char *position = s;
        s += strcspn( s, delim );

        size_t length = s - position;

        if ( minmax.min.position == NULL )
        {
            minmax.min.position = ( char * )position;
            minmax.min.length = length;
            minmax.max.position = ( char * )position;
            minmax.max.length = length;
        }
        else if ( minmax.max.length < length )
        {
            minmax.max.position = ( char * )position;
            minmax.max.length = length;
        }
        else if ( length < minmax.min.length )
        {
            minmax.min.position = ( char * )position;
            minmax.min.length = length;
        }
    }

    return minmax;
}

int main(void) 
{
    const char *s = "alex keep all money";

    struct MinMax minmax = minmax_string( s, " \t" );

    printf( "The minimal string is \"%.*s\" at position %zu\n", 
        ( int )minmax.min.length, minmax.min.position, ( size_t )( minmax.min.position - s ) );
    printf( "The maximum string is \"%.*s\" at position %zu\n", 
        ( int )minmax.max.length, minmax.max.position, ( size_t )( minmax.max.position - s ) );
}

程序输出是

The minimal string is "all" at position 10
The maximum string is "money" at position 14

如果要使用标准C函数ISSPACE,那么函数minmax_string可以查看以下方式,

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

struct SubString
{
    char *position;
    size_t length;
};

struct MinMax
{
    struct SubString min;
    struct SubString max;
};

struct MinMax minmax_string( const char *s )
{
    struct MinMax minmax = 
    { 
        .min = { .position = NULL, .length = 00 }, 
        .max = { .position = NULL, .length = 0 } 
    };

    do
    {
        while ( isspace( ( unsigned char )*s )) ++s;

        if ( *s )
        {
            const char *position = s;

            while ( *s && !isspace( ( unsigned char )*s ) ) ++s;

            size_t length = s - position;

            if ( minmax.min.position == NULL )
            {
                minmax.min.position = ( char * )position;
                minmax.min.length = length;
                minmax.max.position = ( char * )position;
                minmax.max.length = length;
            }
            else if ( minmax.max.length < length )
            {
                minmax.max.position = ( char * )position;
                minmax.max.length = length;
            }
            else if ( length < minmax.min.length )
            {
                minmax.min.position = ( char * )position;
                minmax.min.length = length;
            }
        }
    } while ( *s );
    

    return minmax;
}

int main(void) 
{
    const char *s = "alex keep all money";

    struct MinMax minmax = minmax_string( s );

    printf( "The minimal string is \"%.*s\" at position %zu\n", 
        ( int )minmax.min.length, minmax.min.position, ( size_t )( minmax.min.position - s ) );
    printf( "The maximum string is \"%.*s\" at position %zu\n", 
        ( int )minmax.max.length, minmax.max.position, ( size_t )( minmax.max.position - s ) );
}

程序输出与上面显示的相同

The minimal string is "all" at position 10
The maximum string is "money" at position 14

The program has undefined behavior at least because the variable x used in this while loop

while(x != lenght + 1)

was not initialized

size_t x;

Another problem is the initial initializing of the variable keep by 1

keep = 1;

This can results for example that for the string like "A BB" the substring with the maximum length will be "A" instead of "BB" provided that initially the variable x was initialized by 0 Or if the string starts from space character ' ' like " A" then the maximum substring will be " " and using the expression x = position_max - max in this for loop

for(x = position_max - max; x != position_max; x++)

again results in undefined behavior.

Also you have to take into account situations when a string contains adjacent white spaces.

I would write a separate function for example as shown in the demonstration program below.

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

struct SubString
{
    char *position;
    size_t length;
};

struct MinMax
{
    struct SubString min;
    struct SubString max;
};

struct MinMax minmax_string( const char *s, const char *delim )
{
    struct MinMax minmax = 
    { 
        .min = { .position = NULL, .length = 00 }, 
        .max = { .position = NULL, .length = 0 } 
    };

    for ( s += strspn( s, delim ); *s; s += strspn( s, delim )  )
    {
        const char *position = s;
        s += strcspn( s, delim );

        size_t length = s - position;

        if ( minmax.min.position == NULL )
        {
            minmax.min.position = ( char * )position;
            minmax.min.length = length;
            minmax.max.position = ( char * )position;
            minmax.max.length = length;
        }
        else if ( minmax.max.length < length )
        {
            minmax.max.position = ( char * )position;
            minmax.max.length = length;
        }
        else if ( length < minmax.min.length )
        {
            minmax.min.position = ( char * )position;
            minmax.min.length = length;
        }
    }

    return minmax;
}

int main(void) 
{
    const char *s = "alex keep all money";

    struct MinMax minmax = minmax_string( s, " \t" );

    printf( "The minimal string is \"%.*s\" at position %zu\n", 
        ( int )minmax.min.length, minmax.min.position, ( size_t )( minmax.min.position - s ) );
    printf( "The maximum string is \"%.*s\" at position %zu\n", 
        ( int )minmax.max.length, minmax.max.position, ( size_t )( minmax.max.position - s ) );
}

The program output is

The minimal string is "all" at position 10
The maximum string is "money" at position 14

If you want to use the standard C function isspace then the function minmax_string can look the following way

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

struct SubString
{
    char *position;
    size_t length;
};

struct MinMax
{
    struct SubString min;
    struct SubString max;
};

struct MinMax minmax_string( const char *s )
{
    struct MinMax minmax = 
    { 
        .min = { .position = NULL, .length = 00 }, 
        .max = { .position = NULL, .length = 0 } 
    };

    do
    {
        while ( isspace( ( unsigned char )*s )) ++s;

        if ( *s )
        {
            const char *position = s;

            while ( *s && !isspace( ( unsigned char )*s ) ) ++s;

            size_t length = s - position;

            if ( minmax.min.position == NULL )
            {
                minmax.min.position = ( char * )position;
                minmax.min.length = length;
                minmax.max.position = ( char * )position;
                minmax.max.length = length;
            }
            else if ( minmax.max.length < length )
            {
                minmax.max.position = ( char * )position;
                minmax.max.length = length;
            }
            else if ( length < minmax.min.length )
            {
                minmax.min.position = ( char * )position;
                minmax.min.length = length;
            }
        }
    } while ( *s );
    

    return minmax;
}

int main(void) 
{
    const char *s = "alex keep all money";

    struct MinMax minmax = minmax_string( s );

    printf( "The minimal string is \"%.*s\" at position %zu\n", 
        ( int )minmax.min.length, minmax.min.position, ( size_t )( minmax.min.position - s ) );
    printf( "The maximum string is \"%.*s\" at position %zu\n", 
        ( int )minmax.max.length, minmax.max.position, ( size_t )( minmax.max.position - s ) );
}

The program output is the same as shown above that is

The minimal string is "all" at position 10
The maximum string is "money" at position 14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文