Why is only the first word capitalizing when using the tocap() function?

后端 未结 6 1826
挽巷
挽巷 2021-01-23 15:16

I did the following to upper case the first letter in each word but it\'s only working on the first word. Could someone explain why?

static void Main(string[] ar         


        
6条回答
  •  -上瘾入骨i
    2021-01-23 16:13

    I guess you'll get this better if you understand actually what you're doing:

       public static string tocap(string s)
        {
    
            // This says: "if s length is 1 then returned converted in upper case" 
            // for instance if s = "a" it will return "A". So far the function is ok.
            if (s.Length == 1) return s.ToUpper();
    
    
            string s1;
            string s2;
    
            // This says: "from my string I want the FIRST letter converted to upper case"
            // So from an input like s = "oscar" you're doing this s1 = "O"
            s1 = s.Substring(0, 1).ToUpper();
    
    
            // finally here you're saying: "for the rest just give it to me all lower case"
            // so for s= "oscar"; you're getting "scar" ... 
            s2 = s.Substring(1).ToLower();
    
    
            // and then return "O" + "scar" that's why it only works for the first 
            // letter.
            return s1+s2;
        }
    

    Now what you have to do is to change you're algorithm ( and then your code ) to do WHAT you intend to do

    You can either "split" your string in parts where an space is found, OR you can go for each character and when you find an space you know the following letter will be the beginning of the word isn't ?

    Try this algorithm-psudo code

    inside_space = false // this flag will tell us if we are inside 
                         // a white space.
    
    for each character in string do
    
    
        if( character is white space ) then 
             inside_space = true // you're in an space...
                                 // raise your flag.
    
    
         else  if( character is not white space AND 
                   inside_space == true ) then 
    
               // this means you're not longer in a space 
               // ( thus the beginning of a word exactly what you want ) 
    
              character = character.toUper()  // convert the current 
                                              // char to upper case
    
              inside_space = false;           // turn the flag to false
                                              // so the next won't be uc'ed
         end
    
         // Here you just add your letter to the string 
         // either white space, upercased letter or any other.
    
         result =  result + character 
    
     end // for
    

    Think about it.

    You'll be doing what you want:

    • Go letter by letter and

    • if you're in a space you put a flag,

    • when you're no longer in space then you are in the beginning of a word, the action to take is convert it to uppercase.

    • For the rest you just append the letter to the result.

    When you're learning to program it is better to start doing the "algorithm" in a paper and once you know it will do what you want, pass it to the programming language in turn.

提交回复
热议问题