Char str[255] = {0}; printf ("Please enter a string: \ n"); scanf ("%[^\n]", str);//means non-' \ n '. This means that the input continues as long as no newline is encountered, and this statement ends when a newline character is encountered. By default, the end of a wrap statement execution is encountered, but the value of STR is just the value before the first space. However, when you press ENTER, SCANF executes and all of the contents including spaces are entered into Str. gets (str); int maxLength = 0, maxindex = 0; int length = 0; int i = 0; while (str[i] = ' MaxL ') { if (str[i]! = ') { length++; } else { if (MaxLength < length) { ength = 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]); }
Programming finds the longest word in a known string, assuming that the string contains only letters and spaces, separating the words with spaces.