P5015 [NOIP2018 Popularity Group] Title Statistics (Java)

[NOIP2018 Popularity Group] Headline Statistics

Topic link: https://www.luogu.com.cn/problem/P5015

Title description

Kaikai just wrote a wonderful composition. How many characters are there in the title of this composition? Note: The title may contain uppercase and lowercase English letters, numeric characters, spaces, and line breaks. When counting headline characters, spaces and line breaks are not counted.

Input format

The input file has only one line, one string ss.

Output format

The output file has only one line and contains an integer, which is the number of characters in the title of the composition (without spaces and line breaks).

Sample input and output

Enter #1

234

Output #1

3

Input #2

About 45

Output #2

4

Instructions/tips

[Instructions for input and output example 1]
There are 3 characters in the title, and these 3 characters are all numeric characters.

[Description of input and output sample 2] There are 5 characters in the title, including 1 uppercase English letter, 1 lowercase English letter, 2 numeric characters, and 1 space. Since spaces are not included in the result, the number of valid characters for the title is 4.

[Data size and convention]
stipulates that ∣s∣ represents the length of the string s (that is, the number of characters and spaces in the string).
For 40% of the data, 1≤∣s∣≤5, ensure that the input is numeric characters and newline characters at the end of the line.
For 80% of the data, 1≤∣s∣≤5, the input may only contain upper and lowercase English letters, numeric characters, and line breaks at the end of the line.
For 100% data, 1≤∣s∣≤5, the input may include upper and lowercase English letters, numeric characters, spaces, and newline characters at the end of the line.

Problem-solving ideas:

Pay attention to the input of the string, use nextLine() to input a line.

code show as below:

import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int cnt = 0;
		String s = sc.nextLine();
		char[] c = s.toCharArray();
		for (int i = 0; i < s.length(); i++) {
    
    
			if (c[i] != ' ')
				cnt++;
		}
		System.out.println(cnt);
	}
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/114378234