首页 > 试题广场 >

字符串最后一个单词的长度

[编程题]字符串最后一个单词的长度
  • 热度指数:1590368 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的若干个单词组成的句子,每个单词均由大小写字母混合构成,单词间使用单个空格分隔。输出最后一个单词的长度。

输入描述:
\hspace{15pt}在一行上输入若干个字符串,每个字符串代表一个单词,组成给定的句子。
\hspace{15pt}除此之外,保证每个单词非空,由大小写字母混合构成,且总字符长度不超过 10^3


输出描述:
\hspace{15pt}在一行上输出一个整数,代表最后一个单词的长度。
示例1

输入

HelloNowcoder

输出

13

说明

\hspace{15pt}在这个样例中,最后一个单词是 \texttt{ ,长度为 13
示例2

输入

A B C D

输出

1
推荐
/*使用动态数组来做,输入的字符串依次存入数组中,
最后返回数组中最后一个元素(字符串)的长度*/
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
	string input;
	vector<string>arr;
    while(cin>>input){
    	arr.push_back(input);
	}
	cout<<arr[arr.size()-1].length()<<endl;		
	return 0;
}

编辑于 2016-08-29 14:07:27 回复(93)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()) {
                String str = scanner.nextLine().trim();
                if(str.contains(" ")){
                    System.out.println(str.substring(str.lastIndexOf (" ")+1).length());
                }else{
                    System.out.println(str.length());
                }
            }
     
    }
}
发表于 2025-03-05 17:47:48 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String input = scanner.nextLine();

        String[] words = input.split(" ");

        String lastWord = words[words.length - 1];

        int length = lastWord.length();

        System.out.println(length);

        scanner.close();
    }
    }
发表于 2025-03-01 09:48:07 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //读取这个字符串
        String sentence = in.nextLine();
        //分割句子,得到单词词组
        String[] words = sentence.split(" ");
        //读这个字符串的最后一个单词
        String lastword = words[words.length-1];
        //最后一个单词长度
        int length = lastword.length();
        //输出
        System.out.println(length);
    }
}


发表于 2025-02-04 16:07:09 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inupt = in.nextLine();
        int lastwordLoc = inupt.lastIndexOf(" ");
        System.out.println(inupt.length()-lastwordLoc-1);
    }
}

发表于 2024-09-10 11:13:42 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String[] arr = in.nextLine().split(" ");
            System.out.println(arr[arr.length-1].length());
        }
    }
}
发表于 2024-09-09 21:53:08 回复(0)
java实现,时间复杂度O(n)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(lengthOfLastWord(scanner.nextLine()));
    }

    public static int lengthOfLastWord(String str) {
        int len = str.length();
        int left = 0;
        for (int i = len - 1; i >= 0; i--) {
            if (str.charAt(i) == ' ') {
                left = i + 1;
                break;
            }
        }
        return len - left;
    }
}


发表于 2024-06-27 21:41:30 回复(0)
这段代码没实现人家5000长度的要求吧?
import java.io.*; 
public class Main {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = System.in;
        int length = 0;
        char c;
        while((c=(char)inputStream.read()) != '\n'){
            length++;
            if(c == ' '){
                length = 0;
            }
        }
        System.out.print(length); 
    }
}



发表于 2024-06-03 16:34:38 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            System.out.println(test(s));
        }
    }

    private static int test(String s){
        if(s.length() == 0)
            return 0;

        for(int i = s.length() - 1; i >= 0; i--){
            if(s.charAt(i) == ' ')
                return s.length() - 1 - i;
        }

        return s.length();
    }
}

发表于 2024-06-01 22:12:55 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
   public static void main(String[] args) {  
        Scanner in = new Scanner(System.in);  
 
        System.out.println("请输入一个字符串:");  
        String string = in.nextLine();  
        in.close(); // 关闭 Scanner 对象  
 
        int lastWordLength = getLastWordLength(string);  
        System.out.println("最后一个单词的长度是:" + lastWordLength);  
    }  
 
    public static int getLastWordLength(String string) {  
        if (string == null || string.trim().isEmpty()) {  
            return -1; // 如果字符串为空或 null,返回 -1  
        }  
 
        String[] words = string.trim().split("\\s+"); // 根据空白字符拆分字符串为单词数组  
        if (words.length == 0) {  
            return -1; // 如果没有单词,返回 -1  
        }  
 
        return words[words.length - 1].length(); // 返回最后一个单词的长度  
    }
    }
发表于 2024-04-30 17:57:31 回复(0)

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 
        String string = in.nextLine();
        String s = string.substring(string.lastIndexOf(" ") + 1);
        System.out.println(s.length());
    }
}

发表于 2024-04-26 23:11:58 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()) {
            String str = in.nextLine();
            int lastIndexOf = str.lastIndexOf(" ");
            System.out.println(str.length() - lastIndexOf - 1);
        }
    }
}

发表于 2024-04-20 11:35:27 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] ss = s.trim().split(" ");
        System.out.println(ss[ss.length - 1].length());
    }
}
编辑于 2024-04-09 10:06:50 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String lineStr = in.nextLine();
            String [] lineContents = lineStr.split(" ");
            System.out.println(lineContents[lineContents.length-1].length());
        }
    }
}
编辑于 2024-03-27 16:19:48 回复(0)
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String word = in.nextLine();
        String[] strArr = word.split("");
        int num = 0;
        int i = strArr.length - 1;
        while (" ".equals(strArr[i])){
            i--;
        }
        for (; i >= 0; i--) {
            if (!" ".equals(strArr[i])) {
                num++;
            } else {
                break;
            }
        }
        System.out.println(num);
    }

编辑于 2024-03-13 17:02:29 回复(0)