2013-10-10 5 views
0
def headAndFoot(s): 
    """precondition: s is a string containing lower-case words and spaces 
    The string s contains no non-alpha characters. 
    postcondition: For each word, capitalize the first and last letter. 
    If a word is one letter, just capitalize it. """ 
    last = len(s) - 1 
    x = s.title() 
    y = x.split() 
    return y 

변경해야 할 사항은 무엇입니까?문자열의 각 단어의 마지막 글자를 대문자로 사용하십시오.

+0

왜 당신이 문자열을 분할하는 무슨 UR 이름은? 그리고 사람들이 그것을 읽을 수 있는지 확인하기 위해 질문을 미리 보지 않은 이유는 무엇입니까? –

+0

좀 더 많은 노력을 보여줄 필요가 있습니다. 어떻게하면 좋을지 그리고 왜 효과가 있다고 생각하는지 설명 할 수 있습니까? 그러면 우리는 당신이 실수를 이해하고 당신을 도울 수 있습니다. – Simon

답변

0

아래 코드를보고 질문의 맥락에서 사용해보십시오.

s = 'The dog crossed the street' 

result = " ".join([x[:-1].title()+x[len(x)-1].upper() for x in s.split()]) 

그런 다음

'ThE DoG CrosseD ThE StreeT' 
+0

'x [len (x) -1]'은'x [-1]'이 될 수 있고, jfyi – OmnipotentEntity

-1

가져 오기는 java.io 모양을 초래할 *;

공용 클래스 ConverT_CapS

{

void main()throws IOException 

{ 

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

    System.out.println("Enter a sentence "); 

    String str = br.readLine(); 

    str= str.toLowerCase(); 

    StringBuffer sb = new StringBuffer(str); 

    for(int i=1;i<str.length()-1;i++) 
    { 
     if(str.charAt(i+1)==' '||str.charAt(i-1)==' ') 
     { 
      char ch= (char)(str.charAt(i)-32); 
      sb.setCharAt(i,ch); 
     } 
    } 
    sb.setCharAt(0,(char)(str.charAt(0)-32)); 
    sb.setCharAt(str.length()-1,(char)(str.charAt(str.length()-1)-32)); 
    System.out.println(sb); 
} 

}

입력 : 무슨 UR 이름 출력 :

관련 문제