Program to find the longest word in a known string. Assume that the string contains only letters and spaces, and use spaces to separate words.
Char str [255] = {0}; printf ("enter a string: \ n"); scanf ("% [^ \ n]", str ); // It indicates '\ n '. That is to say, the input will continue as long as there is no line feed. When there is a line break, this statement ends. By default, the execution of a line feed statement ends, but the str value is only the value before the first space. In this case, scanf is executed when you press enter, and all content including spaces in the middle will be input to str. // Gets (str); int maxLength = 0, maxIndex = 0; int length = 0; int I = 0; while (str [I]! = '\ 0') {if (str [I]! = '') {Length ++;} else {if (maxLength <length) {maxLength = length; maxIndex = I-maxLength;} length = 0;} I ++ ;} if (maxLength <length) {maxLength = length; maxIndex = I-maxLength;} for (int j = maxIndex; j <maxIndex + maxLength; j ++) {printf ("% c", str [j]);}