Полагаю, вам станет лучше, если вы действительно поймете, что делаете:
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;
}
Теперь вам нужно изменить алгоритм (а затем и ваш код) на то, ЧТО вы собираетесь делать
Вы можете «разбить» вашу строку на части, где найден пробел, ИЛИ вы можете перейти к каждому символу, и когда вы найдете пробел, вы знаете, что следующая буква будет началом слова, не так ли?
Попробуйте этот алгоритм-псевдо-код
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
Подумай об этом.
Вы будете делать то, что хотите:
Go буква за буквой и
если вы находитесь в пространстве, вы ставите флаг,
когда вы больше не в космосе, то находитесь в начале слова, действие, которое нужно выполнить, - преобразовать его в верхний регистр.
В остальном вы просто добавляете письмо к результату.
Когда вы учитесь программировать, лучше начать выполнять «алгоритм» на бумаге, и как только вы узнаете, что он будет делать то, что вам нужно, по очереди передайте его языку программирования.