tags:

views:

471

answers:

7

I did the following to upper case the first letter in each word but its working only on the first word

check it out Why ?

and please explain

static void Main(string[] args)
        {
            string s = "how u doin the dnt know about medri sho min shan ma baref shu";
            string a = tocap(s);
            Console.WriteLine(a);
        }

        public static string tocap(string s)
        {

            if (s.Length == 1) return s.ToUpper();
            string s1;
            string s2;

            s1 = s.Substring(0, 1).ToUpper();
            s2 = s.Substring(1).ToLower();



            return s1+s2;
        }
+1  A: 

You somehow need to tokenize your initial string. You're currently not even looking past the first character of the whole thing.

Chris Farmer
+7  A: 

Learn to love the string.split() method.

Any more help than that and I would feel dirty.

Triptych
A: 

Use string.Split(' ') to break up the sentence into a bunch of words than use the code you have to capitalize each word... then put it all back together.

jhunter
+9  A: 

Since no professor would accept this solution, I feel fine letting anyone googling this know that you can just use ToTitleCase

Tom Ritter
Although this is the correct solution I guess it won't help at all the the questioner.
OscarRyz
+3  A: 

Try using:

System.Globalization.TextInfo.ToTitleCase

steve_c
+1 this should've been the accepted answer!
drusnov
+7  A: 

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.

OscarRyz
thanx that was useful
I'm glad. After several year programming, some of us just forget how hard was to pass from problem statement "Create an application that reads and strig and blah, blah, blah" to code " public string s = ""... Here are some "additional" strategies: http://stackoverflow.com/questions/137375
OscarRyz
Yeah, the important thing here is to break the problem into the smallest possible pieces and examine each problem logically.Hope you get a A+ and a gold start for this one Oscar.
James McMahon
heheh, Thank you nemo. Now I just need another 97 uv
OscarRyz
Above and beyond the call of duty, Oscar. Nice description here.
Chris Farmer
@Chris, thank you. 94 more :)
OscarRyz
93 and counting.
OscarRyz
A: 

There is also the style tag in HTML, if that is your output format;

text-transform:capitalize
Amadiere