题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
http://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
import java.util.Scanner;
public class Main {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int index = -1;
for(int i = s.length()-1;i>=0;i--){
if(s.charAt(i) == ' '){
index = i;
break;
}
}
System.out.println(s.length() - index-1);
}
}
public class Main {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int index = -1;
for(int i = s.length()-1;i>=0;i--){
if(s.charAt(i) == ' '){
index = i;
break;
}
}
System.out.println(s.length() - index-1);
}
}