参数解析
参数解析__牛客网
输入:输入一行字符串,可以有空格
xcopy /s c:\\ d:\\e
输出:输出参数个数,分解后的参数,每个参数都独占一行
4
xcopy
/s
c:\\
d:\\e
注意:对于用""包含起来的参数,如果中间有空格,不能解析为多个参数。
"xcopy /s "C:\\program files" d:\"时,参数仍然是4个,第3个参数应该是字符串C:\\program files.
public class Testdome {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s= sc.nextLine();
int count=0;
for (int i = 0; i < s.length(); i++) {
//统计字符串输出个数
if (s.charAt(i)=='"'){//遍历双引号里面字符串
do {//do while语句就是至少会先执行一次
i++;
}while (s.charAt(i)!='"');//不是双引号就++
}
//双引号以外空格
if(s.charAt(i)==' '){
count++;
}
}
System.out.println(count+1);
//输出字符串
//标记双引号
int flg=1;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)=='"'){
flg^=1;
}
if (s.charAt(i)!=' '&&s.charAt(i)!='"'){//遍历不是空格和双引号都要输出
System.out.print(s.charAt(i));
}
if (s.charAt(i)==' '&&flg==1){//没在双引号里面的空格不输出,直接换行
System.out.println();
}
if (s.charAt(i)==' '&&flg==0){//双引号里面的空格要输出
System.out.println(s.charAt(i));
}
}
}