fork download
  1. using System.Text;
  2.  
  3. public class Test {
  4. public static void Main() {
  5. System.Console.WriteLine("|" + Capitalizar(" JOSÉ DA COSTA DOS PEREIRAS E SILVA ") + "|");
  6. }
  7. static string Capitalizar(string texto) {
  8. var novo = new StringBuilder(texto.Length);
  9. for (var i = 0; i < texto.Length; i++) {
  10. var atual = char.ToLower(texto[i]);
  11. var anterior = i == 0 ? ' ' : char.ToLower(texto[i - 1]);
  12. var proximo = i > texto.Length - 2 ? 0 : char.ToLower(texto[i + 1]);
  13. var segundo = i > texto.Length - 3 ? 0 : char.ToLower(texto[i + 2]);
  14. var terceiro = i > texto.Length - 4 ? 0 : char.ToLower(texto[i + 3]);
  15. if (atual == ' ' && anterior == ' ') continue;
  16. if (anterior != ' ' ||
  17. (atual == 'e' && proximo == ' ') ||
  18. (atual == 'd' &&
  19. ((proximo == 'a' || proximo == 'e' || proximo == 'o') && (segundo == ' ')) ||
  20. ((proximo == 'a' || proximo == 'o') && (segundo == 's') && (terceiro == ' '))
  21. )) novo.Append(atual);
  22. else novo.Append(char.ToUpper(atual));
  23. }
  24. if (novo[novo.Length - 1] == ' ') novo.Remove(novo.Length - 1, 1);
  25. return novo.ToString();
  26. }
  27. }
  28.  
  29. //https://pt.stackoverflow.com/q/247/101
Success #stdin #stdout 0.06s 26620KB
stdin
Standard input is empty
stdout
|José da Costa dos Pereiras e Silva|