[Java data structure and algorithm practice] the length of the last word of HJ1 string

This article was first published from "MOOC.com". If you want to know more about IT dry goods and hot news in the programmer circle, please pay attention to "MOOC.com" and "MOOC.com Official Account"!

Author: Lao Wei | MOOC famous teacher


describe

Calculate the length of the last word of the string, the words are separated by spaces, and the length of the string is less than 5000. (Note: The end of the string does not end with a space)

Enter a description:

Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

Output description:

Output an integer representing the length of the last word of the input string.

solution

This question type mainly examines the operation of strings. The solution steps are as follows

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Group the input string by whitespace

  • Get the last element in the data set

  • Output an integer representing the length of the last word of the input string.

code show as below:

 * Copyright (c) waylau.com, 2022. All rights reserved.
 */ 
package com.waylau.nowcoder.exam.oj.huawei;import java.util.Scanner;/**
 * HJ1 字符串最后一个单词的长度.
 * 描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
 * 输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。
 * 输出描述:输出一个整数,表示输入字符串最后一个单词的长度。
 *
 * @author <a href="https://waylau.com">Way Lau</a>
 * @since 2022-08-05
 */public class HJ1LengthOfTheLastWordInTheString {

    public static void main(String[] args) {        // 输入一行,代表要计算的字符串,非空,长度小于5000。
        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();        
        // 按照空格对输入字符串进行分组
        String[] words = in.split(" ");        
        // 取数据组中最后一个元素
        String lastWord = words[words.length - 1];        
        // 输出一个整数,表示输入字符串最后一个单词的长度。
        System.out.println(lastWord.length());        
        // 关闭资源
        sc.close();
    }
}

The split and length methods of String are used, and arrays are also used. The result of running the code is as follows.

hello world

5

Solution 2

Operations on characters are also possible. The solution steps are as follows

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Traversing the input string in reverse order, stopping at the first space

  • The number of non-empty characters traversed is the length of the last word

  • Output an integer representing the length of the last word of the input string.

 * Copyright (c) waylau.com, 2022. All rights reserved.
 */ 
package com.waylau.nowcoder.exam.oj.huawei;import java.util.Scanner;/**
 * HJ1 字符串最后一个单词的长度.
 * 描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
 * 输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。
 * 输出描述:输出一个整数,表示输入字符串最后一个单词的长度。
 *
 * @author <a href="https://waylau.com">Way Lau</a>
 * @since 2022-08-05
 */public class HJ1LengthOfTheLastWordInTheString2 {

    public static void main(String[] args) {        // 输入一行,代表要计算的字符串,非空,长度小于5000。
        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();        
        // 对输入字符串进行倒序遍历,遇到第一个空格则停止.
        // 遍历的非空字符数即为最后一个单词的长度
        int lastWordLength = 0;        for (int i = in.length() -1 ; i >=0; i--) {            char c = in.charAt(i);            
            if (c == ' ') {                break;
            }
            
            lastWordLength ++;
        }        
        // 输出一个整数,表示输入字符串最后一个单词的长度。
        System.out.println(lastWordLength);        
        // 关闭资源
        sc.close();
    }
}

 

  • describe

  • Calculate the length of the last word of the string, the words are separated by spaces, and the length of the string is less than 5000. (Note: The end of the string does not end with a space)

  • Enter a description:

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Output description:

  • Output an integer representing the length of the last word of the input string.

  • solution

  • This question type mainly examines the operation of strings. The solution steps are as follows

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Group the input string by whitespace

  • Get the last element in the data set

  • Output an integer representing the length of the last word of the input string.

  • code show as below:

  •  * Copyright (c) waylau.com, 2022. All rights reserved.
     */ 
    package com.waylau.nowcoder.exam.oj.huawei;import java.util.Scanner;/**
     * HJ1 字符串最后一个单词的长度.
     * 描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
     * 输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。
     * 输出描述:输出一个整数,表示输入字符串最后一个单词的长度。
     *
     * @author <a href="https://waylau.com">Way Lau</a>
     * @since 2022-08-05
     */public class HJ1LengthOfTheLastWordInTheString {
    
        public static void main(String[] args) {        // 输入一行,代表要计算的字符串,非空,长度小于5000。
            Scanner sc = new Scanner(System.in);
            String in = sc.nextLine();        
            // 按照空格对输入字符串进行分组
            String[] words = in.split(" ");        
            // 取数据组中最后一个元素
            String lastWord = words[words.length - 1];        
            // 输出一个整数,表示输入字符串最后一个单词的长度。
            System.out.println(lastWord.length());        
            // 关闭资源
            sc.close();
        }
    }

The split and length methods of String are used, and arrays are also used. The result of running the code is as follows.

hello world

5

Solution 2

     Operations on characters are also possible. The solution steps are as follows

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Traversing the input string in reverse order, stopping at the first space

  • The number of non-empty characters traversed is the length of the last word

  • Output an integer representing the length of the last word of the input string.

  •  * Copyright (c) waylau.com, 2022. All rights reserved.
     */ 
    package com.waylau.nowcoder.exam.oj.huawei;import java.util.Scanner;/**
     * HJ1 字符串最后一个单词的长度.
     * 描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
     * 输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。
     * 输出描述:输出一个整数,表示输入字符串最后一个单词的长度。
     *
     * @author <a href="https://waylau.com">Way Lau</a>
     * @since 2022-08-05
     */public class HJ1LengthOfTheLastWordInTheString2 {
    
        public static void main(String[] args) {        // 输入一行,代表要计算的字符串,非空,长度小于5000。
            Scanner sc = new Scanner(System.in);
            String in = sc.nextLine();        
            // 对输入字符串进行倒序遍历,遇到第一个空格则停止.
            // 遍历的非空字符数即为最后一个单词的长度;田田
            int lastWordLength = 0;        for (int i = in.length() -1 ; i >=0; i--) {            char c = in.charAt(i);            
                if (c == ' ') {                break;
                }
                
                lastWordLength ++;
            }        
            // 输出一个整数,表示输入字符串最后一个单词的长度。
            System.out.println(lastWordLength);        
            // 关闭资源
            sc.close();
        }
    }

describe

Calculate the length of the last word of the string, the words are separated by spaces, and the length of the string is less than 5000. (Note: The end of the string does not end with a space)

Enter a description:

Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

Output description:

Output an integer representing the length of the last word of the input string.

solution

This question type mainly examines the operation of strings. The solution steps are as follows

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Group the input string by whitespace

  • Get the last element in the data set

  • Output an integer representing the length of the last word of the input string.

code show as below:

 * Copyright (c) waylau.com, 2022. All rights reserved.
 */ 
package com.waylau.nowcoder.exam.oj.huawei;import java.util.Scanner;/**
 * HJ1 字符串最后一个单词的长度.
 * 描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
 * 输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。
 * 输出描述:输出一个整数,表示输入字符串最后一个单词的长度。
 *
 * @author <a href="https://waylau.com">Way Lau</a>
 * @since 2022-08-05
 */public class HJ1LengthOfTheLastWordInTheString {

    public static void main(String[] args) {        // 输入一行,代表要计算的字符串,非空,长度小于5000。
        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();        
        // 按照空格对输入字符串进行分组
        String[] words = in.split(" ");        
        // 取数据组中最后一个元素
        String lastWord = words[words.length - 1];        
        // 输出一个整数,表示输入字符串最后一个单词的长度。
        System.out.println(lastWord.length());        
        // 关闭资源
        sc.close();
    }
}

The split and length methods of String are used, and arrays are also used. The result of running the code is as follows.

hello world

5

Solution 2

Operations on characters are also possible. The solution steps are as follows

  • Enter a line, representing the string to be calculated, non-empty, and the length is less than 5000.

  • Traversing the input string in reverse order, stopping at the first space

  • The number of non-empty characters traversed is the length of the last word

  • Output an integer representing the length of the last word of the input string.

 * Copyright (c) waylau.com, 2022. All rights reserved.
 */ 
package com.waylau.nowcoder.exam.oj.huawei;import java.util.Scanner;/**
 * HJ1 字符串最后一个单词的长度.
 * 描述:计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
 * 输入描述:输入一行,代表要计算的字符串,非空,长度小于5000。
 * 输出描述:输出一个整数,表示输入字符串最后一个单词的长度。
 *
 * @author <a href="https://waylau.com">Way Lau</a>
 * @since 2022-08-05
 */public class HJ1LengthOfTheLastWordInTheString2 {

    public static void main(String[] args) {        // 输入一行,代表要计算的字符串,非空,长度小于5000。
        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();        
        // 对输入字符串进行倒序遍历,遇到第一个空格则停止.
        // 遍历的非空字符数即为最后一个单词的长度
        int lastWordLength = 0;        for (int i = in.length() -1 ; i >=0; i--) {            char c = in.charAt(i);            
            if (c == ' ') {                break;
            }
            
            lastWordLength ++;
        }        
        // 输出一个整数,表示输入字符串最后一个单词的长度。
        System.out.println(lastWordLength);        
        // 关闭资源
        sc.close();
    }
}

Welcome to pay attention to the official account of "MOOC". We will always insist on providing high-quality content in the IT circle, sharing dry knowledge, and let's grow together!

This article was originally published on Muke.com, please indicate the source for reprinting, thank you for your cooperation

Guess you like

Origin blog.csdn.net/mukewangguanfang/article/details/130620148