您的位置:首页 > 其它

将一段英文每个单词首字母大写

2016-08-13 13:43 423 查看
     Have the function LetterCapitalize(str) take the str parameter being

      passed and capitalize the first letter of each word. Words will be separated

      by only one space.

      Use the Parameter Testing feature in the box below to test your code with

      different arguments.

 

package demo5;

import java.util.Scanner;

class Test3 {  

  public static String LetterCapitalize(String str) {

 

    str.trim();

    int wordNum=1;

    boolean flag=false;

    String str2="";

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

        char indexword=str.charAt(i);

        if(i==0 || flag==true){

         if(indexword>='a' && indexword<='z'){

            str2+=(char)(indexword-32);

        }else{

                str2+=indexword;

        }

            flag=false;

        }else if(indexword==' '){

            str2+=' ';

            flag=true;

        }else{

            str2+=indexword;

            flag=false;

        }

        

    }

    return str2;

    

  }

 

  public static void main (String[] args) {  

    // keep this function call here     

    Scanner s = new Scanner(System.in);

    System.out.print(LetterCapitalize(s.nextLine()));

  }   

 

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: