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