链接:参数解析__牛客网
来源:牛客网
[编程题]参数解析
- 热度指数:100294 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
在命令行输入如下命令:
xcopy /s c:\\ d:\\e,
各个参数如下:
参数1:命令字xcopy
参数2:字符串/s
参数3:字符串c:\\
参数4: 字符串d:\\e
请编写一个参数解析程序,实现将命令行各个参数解析出来。
解析规则:
1.参数分隔符为空格
2.对于用""包含起来的参数,如果中间有空格,不能解析为多个参数。比如在命令行输入xcopy /s "C:\\program files" "d:\"时,参数仍然是4个,第3个参数应该是字符串C:\\program files,而不是C:\\program,注意输出参数时,需要将""去掉,引号不存在嵌套情况。
3.参数不定长4.输入由用例保证,不会出现不符合要求的输入
数据范围:字符串长度:1≤s≤1000 1\le s\le 1000\ 1≤s≤1000
进阶:时间复杂度:O(n) O(n)\ O(n) ,空间复杂度:O(n) O(n)\ O(n)
输入描述:
输入一行字符串,可以有空格
输出描述:
输出参数个数,分解后的参数,每个参数都独占一行
示例1
输入
xcopy /s c:\\ d:\\e输出
4 xcopy /s c:\\ d:\\e
- 全部代码
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int count = 1;
int f = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '"') {
f++;
}
if (str.charAt(i) == ' ' && f%2 ==0) {
count++;
}
}
System.out.println(count);
int flag = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '"') {
flag ++;
}
if(str.charAt(i) != ' ' && str.charAt(i) != '"'){
System.out.print(str.charAt(i));
}
if(str.charAt(i) == ' ' && flag%2 != 0){
System.out.print(str.charAt(i));
}
if(str.charAt(i) == ' ' && flag%2 == 0){
System.out.println();
}
}
}
}