以小写形式打印输入字符串的第二个单词的程序 [英] Program to print the second word of the input string in lower case

查看:81
本文介绍了以小写形式打印输入字符串的第二个单词的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我正在使用< string> .length();函数来查找字符串的长度。但是,该功能仅将字符计数到空格键。我需要它来计算它直到字符串的结尾。我该怎么办?



我的尝试:



/ / program用小写打印输入字符串的第二个单词

包混合;

import java.util.Scanner;

公共类Mixed7 {

public static void main(String [] args){

Scanner user_input = new Scanner(System.in);



String str = user_input.next(); //字符串初始化

char [] str_c; //字符数组(我们将< str>字符串转换为此数组)

str_c = new char [40]; //我不认为我必须解释这个



int i = 0; //用于循环的整数



//System.out.println(str.length()); //用它来查找字符串的长度



for(i = 1; i< str.length(); i ++){

str_c [i] = str.charAt(i); //使用.charAt()函数将字符串中的字符发送到字符数组

}



int second_word = 0;



for(i = 0; i< str.length(); i ++)//

if(str_c [i] == '')//使用这个我找到空格的字符,下一个字符将是数组的第二个字

second_word = i ++; //



String str2;

str2 = str.substring(second_word).toLowerCase(); //我将字符串的第二个字输入str2变量

System.out.println(str2); //我打印第二个单词



}

}

The problem is that i am using the <string>.length(); function to find the length of the string. However the function only counts the character upto the space bar. I need it to count it till the end of the string. What should I do?

What I have tried:

//program to print the second word of the input string in lower case
package mixed;
import java.util.Scanner;
public class Mixed7 {
public static void main(String[] args){
Scanner user_input=new Scanner(System.in);

String str=user_input.next(); //string initialization
char[] str_c; //character array (we will convert the <str>string to this array)
str_c=new char[40]; //i dont think i have to explain this

int i=0; //integer for for looping

//System.out.println(str.length()); //used this to find the length of string

for(i=1;i<str.length();i++){
str_c[i]=str.charAt(i); //using the .charAt() function to send the characters in ths string to the character array
}

int second_word = 0;

for(i=0;i<str.length();i++) //
if(str_c[i]==' ') //using this I find the character that is space and the next character will be the second word of the array
second_word=i++; //

String str2;
str2=str.substring(second_word).toLowerCase(); //I pput the second word of the string into the str2 variable
System.out.println(str2); //I print the second word

}
}

推荐答案

使用StringTokenizer可以检索第二个单词。
Using StringTokenizer you can retrieve the second word.


使用 Scanner.nextLine() [ ^ ]获取完整输入文本的方法。
Use the Scanner.nextLine()[^] method to get the complete input text.


这篇关于以小写形式打印输入字符串的第二个单词的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆