{ "type": "code_options", "author": "https://github.com/begeekmyfriend/leetcode", "source": "solution.md", "exercise_id": "a32346e4fd59416c86e6fa9fabaa217a", "keywords": "字符串", "title": "最后一个单词的长度", "desc": [ { "content": "\n

给你一个字符串 s,由若干单词组成,单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词,请返回 0 。

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

 

示例 1:

输入:s = \"Hello World\"
输出:
5

示例 2:

输入:s = \" \"
输出:
0

 

提示:

", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "#include \n#include \nint lengthOfLastWord(char *s)\n{\n\tint len = 0;\n\twhile (*s != '\\0')\n\t{\n\t\tif (s[-1] == ' ' && s[0] != ' ')\n\t\t{\n\t\t\tlen = 1;\n\t\t}\n\t\telse if (*s != ' ')\n\t\t{\n\t\t\tlen++;\n\t\t}\n\t\ts++;\n\t}\n\treturn len;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test word\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%d\\n\", lengthOfLastWord(argv[1]));\n\treturn 0;\n}", "language": "cpp" }, "node_id": "dailycode-1f7753ab85df4d1d9a4479becc5c6b4d", "license": "csdn.net", "created_at": 1637894160, "topic_link": "https://bbs.csdn.net/topics/600470793" }