Finding longest word in string

后端 未结 4 1334
醉梦人生
醉梦人生 2021-01-19 04:13

Ok, so I know that questions LIKE this have been asked a lot on here, but I can\'t seem to make solutions work. I am trying to take a string from a file and find the longest

4条回答
  •  醉酒成梦
    2021-01-19 05:00

    As suggested in the other answer, you need to split your string.

    string[] stringOfWords = fileText.split(new Char [] {',' , ' ' });
    //all is well, now let's loop over it and see which is the biggest
    int biggest = 0;
    int biggestIndex = 0;
    
    for(int i=0; i

    What we're doing here is splitting the string based on whitespace (' '), or commas- you can add an unlimited number of delimiters there - each word, then, gets its own space in the array.

    From there, we're iterating over the array. If we encounter a word that's longer than the current longest word, we update it.

提交回复
热议问题