Question 12th: length of the last word

Source: Internet
Author: User

Question 12th: length of the last word


Returns the length of the last word given a string consisting of upper-case, lower-case letters, and spaces.

If the last word does not exist, 0 is returned.

Note:

A word is a string that does not contain space characters.

For example:

S = "Hello World", the returned result is 5

Format:

Enter the string s in the first line, and then output the length of the last word in S.

Sample Input

Today is a nice day

Sample output

3


About this question:

There is a trap in this question: S = "Hello World", which may also be S = "Hello World" (note the last space). At first I didn't pay attention to this, the result is too long to find the problem. Another point is that the array declared during these questions should be as large as possible, if a [100],A [1000] is certainly unable to communicate with each other. If the question is not specified, the test scope is a defect.


Resolution:

# Include <stdio. h> # include <string. h> int main () {char s [10000]; int COUNT = 0, I, C; gets (s); for (I = strlen (S)-1; i> 0; I --) {If (s [I]! = '') // From the back, when the last one is not a space, count ++; If (count> 0) // when count is counted, the word at the position is already the last one. When a space is displayed, it indicates that the end of IF (s [I] = '') break ;} printf ("% d \ n", count); Return 0 ;}

If you cannot understand it, please leave a message or leave an email !!! O (partition _ partition) O

(Leave a message if you need an invitation code)

Question 12th: length of the last word

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.