简体   繁体   English

字符串最长

[英]Longest in string

#include <stdio.h>
#include <string.h>
int main()
{
    char string[100], word[20], max[20];
    int i = 0, j = 0, flag = 0;
    printf("Enter string: ");
    gets(string);
    for (i = 0; i < strlen(string); i++)
    {
        while (i < strlen(string) && string[i]!=32 && string[i]!=0)  //line 1
        {
            word[j++] = string[i++];
        }
        if (j != 0)
        {
            word[j] = '\0';
            if (!flag)
            {
                flag = !flag;
                strcpy(max, word);
            }
            if (strlen(word) > strlen(max))
            {
                strcpy(max, word);
            }
            j = 0;
        }
    }
    printf("The largest word is '%s' .\n", max);
    return 0;
}

I came across this code that finds the longest word in a given string and returns the last one in case of multiple occurrences of same length word 我遇到了这个代码,它找到给定字符串中最长的单词,并在多次出现相同长度单词的情况下返回最后一个单词

(1) I do not understand why output does not depend upon max[], I mean finally we are printing the longest word out of max[] and its size is max[20] but even for very large words it gives correct output, changing this to max[10] also works. (1)我不明白为什么输出不依赖于max [],我的意思是最后我们打印max []中最长的单词并且它的大小是max [20]但是即使对于非常大的单词它也能提供正确的输出,将其更改为max [10]也有效。

(2) In line 1 why we are testing string[i]!=0 since removing this has no effect and we have already tested i < strlen(string) in first part of while loop. (2)在第1行中为什么我们测试string[i]!=0因为删除它没有效果,我们已经在while循环的第一部分测试了i < strlen(string) But when i try string[i]!='\\0' i get incorrect output for some input strings. 但是当我尝试string[i]!='\\0'我的某些输入字符串输出不正确。

Where am i missing the logic for these two? 我在哪里错过了这两个的逻辑?

@callyalater, I put max[] before word[] and string[] but it has no effect on output but for max[5] and input string "arrenhius equation is hard to decipher zxcvbnmlkjhgfdsaqwertyuiop" the output is 'arrenzxcvbnmlkjhgfdsaqwertyuiop' on the online compiler link provided but works fine for max[10] but in my compiler(dev c++) the output gives correct results for max[5] and max[10] both which is 'zxcvbnmasdfghjklqwertyuiop'. @callyalater,我把max []放在word []和string []之前,但它对输出没有影响但是对于max [5]和输入字符串“arrenhius等式很难破译zxcvbnmlkjhgfdsaqwertyuiop”输出是'arrenzxcvbnmlkjhgfdsaqwertyuiop'在线提供的编译器链接但是对于max [10]工作正常,但在我的编译器(dev c ++)中,输出给出max [5]和max [10]的正确结果,这两个都是'zxcvbnmasdfghjklqwertyuiop'。

char string[100], word[20], max[20];
int i = 0, j = 0, flag = 0;

Over-running max, will modify i or word 过度运行max,将修改i或word

The memory could be used as below 存储器可以如下使用

+-------+-------------+-------+
| word  | max         | i     |
+-------+-------------+-------+

or 要么

+-------+-------------+-------+
| i     | max         | word  |
+-------+-------------+-------+

So writing more than 20 bytes into max, could modify word, or i - as it overruns. 所以写入超过20个字节到最大值,可以修改单词,或者我 - 因为它超出了。

When you overrun these memory items, you enter undefined behavior, which may work (as you found) or crash. 当您超出这些内存项时,您输入未定义的行为,这可能有效(如您找到)或崩溃。

The string[i] !=0 is protecting against some of these failings. string[i] !=0可以防止其中一些失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM