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.

Source: Internet
Author: User

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]);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.