Title: Given a string, the length of the last word, with spaces in the middle of each word.
For example: input: Hello World output: 5
C Code: Pass.
#include <stdio.h>#define MAXN1000#include<string.h>intMain () {intNumber = 0, I,len; CharS[MAXN]; Gets (s); Len=strlen (s); for(i = len-1; I >= 0; i--) { if(S[i] = = ") { number= Len-1-i; printf ("%d\n", number); return1; }} printf ("%d\n", Len); return0;}
Java code: not passed
Import java.util.scanner;//Enter a string of characters, the length of its last word public class Lastword {public static void main (string[] args) {Scanner Scanner=new Scanner (system.in); System.out.println ("Please enter a serial number:"); String s=scanner.nextline (); Scanner.close (); String[] Ss=s.split (""); if (ss.length==0) System.out.println (S.length ()); ElseSystem.out.println (Ss[ss.length-1]. Length ());}}
hwoj-to find the length of the last word in a string