首页 > 试题广场 >

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

[编程题]字符串最后一个单词的长度
  • 热度指数:1490007 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾

输入描述:

输入一行,代表要计算的字符串,非空,长度小于5000。



输出描述:

输出一个整数,表示输入字符串最后一个单词的长度。

示例1

输入

hello nowcoder

输出

8

说明

最后一个单词为nowcoder,长度为8   
推荐
/*使用动态数组来做,输入的字符串依次存入数组中,
最后返回数组中最后一个元素(字符串)的长度*/
#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;

// 注意类名必须为 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)
import java.util.Scanner;

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

编辑于 2024-03-02 22:30:08 回复(0)
Scanner sc = new Scanner(System.in);
String inStr = sc.nextLine(); if (inStr.length() >= 5000) return;
String s = inStr.substring(inStr.lastIndexOf(" ") + 1);//从最后一个空格的索引+1开始取字符串 System.out.println(s.length());
sc.close();
编辑于 2024-01-12 19:24:50 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
      String input = scanner.nextLine();
       //读取一行字符串
      String[] words = input.split(" ");
      //按空格分割字符串,得到单词组
      int length = 0;
      if(words.length > 0){
        length = words[words.length - 1].length();
      }
      System.out.println(length);

    }
}
发表于 2024-01-09 14:54:34 回复(0)
public static void main(String[] args) {  Scanner scanner = new Scanner(System.in);  String[] s = scanner.nextLine().split(" ");  int length = s[s.length - 1].length();  System.out.println("最后一个单词的长度是:"+length);  }
发表于 2023-11-06 20:05:41 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strs = in.nextLine().split(" ");
        System.out.print(strs[strs.length-1].length());
    }
}
发表于 2023-10-29 16:06:13 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        if (str != null || str.length() != 0) {
            String[] str1 = str.split(" ");
            int a = str1.length;
            System.out.print(str1[a - 1].length());
        }else System.out.print(0);
    }
}
发表于 2023-08-28 15:31:16 回复(0)

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息

public class Main {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    String str = " " + in.nextLine();

    int endIdx = str.length();

    // 注意 hasNext 和 hasNextLine 的区别

    for (int i = str.length() - 1; i >= 0; i--) {

        if (str.charAt(i) == ' ' || i == 0) {

            endIdx = i;

            break;

        }

    }

    System.out.println(str.length() - endIdx - 1);

}

}

发表于 2023-08-08 17:04:12 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            String line = scanner.nextLine();
            int lengthResult = line.length() - line.lastIndexOf(" ") - 1;
            System.out.println(lengthResult);
        }
    }
}
发表于 2023-08-06 09:08:41 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        String[] res = str.split(" ");
        System.out.println(res[res.length-1].length());
    }
}
发表于 2023-07-22 12:11:13 回复(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 的区别
         String a = in.next();
       
        while (in.hasNext()) { // 注意 while 处理多个 case
           String  b = in.next();
           a=b;
        }
        System.out.println(a.length());
    }
}
发表于 2023-07-10 22:18:44 回复(0)
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine().trim(); // 去除字符串前后的空格
        int len = 0;
        for (int i = str.length() - 1; i >= 0; i--) { // 从后往前遍历字符串
            if (str.charAt(i) != ' ') { // 如果当前字符不是空格,则累加单词长度
                len++;
            } else if (len > 0) { // 如果当前字符是空格且单词长度大于0,则表示已经统计完最后一个单词的长度
                break;
            }
        }
        System.out.println(len);
        scanner.close();
    }
}
发表于 2023-06-09 17:32:42 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str="";
        str=in.nextLine();
        int sum=0;
        for(int i=str.length()-1;i>=0;i--){
            if(str.charAt(i)==' '){
                System.out.print(sum);
                return;
            }else{
                sum++;
            }
        }
        System.out.print(sum);
    }
}

发表于 2023-05-24 11:21:59 回复(0)