Naprzemienność dużych i małych liter

0

Witam.

Próbuję napisać bardzo prosty program do którego wklepuje się tekst, a on zwraca ten samy tekst, ale z naprzemiennie występującymi małymi i dużymi literami, np. wprowadzam "Lubie placki", a on zwraca "LuBiE PlAcKi".

Właściwie to napisałem go jak umiałem, ale jest błąd z którym nie mogę sobie poradzić.

Miejsca które pogrubiłem są podkreślone a przy nich błąd o treści: "Operator '+' cannot be applied to operands of type 'string' and 'method group'.

Próbowałem to jakoś modyfikować, robić na inne sposoby, ale za każdym razem jest jakiś błąd - ten albo inny..

Proszę o pomoc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ConsoleApplication54
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = Console.ReadLine();
            int value = 1;
            string result = "";
            

            foreach (var i in Enumerable.Range(0,text.Length-1))
            {
                
                if (value == 1)
                {
                    
                    result =** result + (Convert.ToString(text[i])).ToUpper;**
                    
                    
                }
                else
                {
                    result = **result + (Convert.ToString(text[i])).ToLower;**
                }
                value = (value + 1)%2;

            }
            Console.WriteLine(result);
            Console.Read();
        }
    }
}
 
0

Nie wywołałeś metod ToUpper i ToLower. Dodaj nawiasy po jej nazwie ;).
I skorzystaj lepiej ze StringBuilder. I lepiej by tutaj pasowała zwykła pętla for.

0
static string GetPokemonString(string input)
        {
            if (string.IsNullOrEmpty(input)) return null;
            input = input.ToLower();

            string output = string.Empty;

            for (int i = 0; i < input.Length; ++i)
            {
                if (i % 2 == 0)
                {
                    output += input[i].ToString().ToUpper();
                }
                else output += input[i];
            }

            return output;
        }
0

Oczywiście, korzystasz z tego na przykład w taki sposób:

static void Main(string[] args)
        {
            Console.WriteLine(GetPokemonString("Prącie (łac. penis a. membrum virile) jest narządem homologicznym żeńskiej łechtaczki."));
            Console.Read();
        }
2

Generalny problem z operacją konkatenacją stringów jest taki, że powoduje ona powstanie kopii całego napisu.

public static class StringExtensions
{
    public static string Pokemonize(this string text)
    {
        if (string.IsNullOrEmpty(text)) return text;

        var result = new StringBuilder(text.Length);
        for (int i = 0; i < text.Length; i++)
            result.Append(i % 2 == 0 ? char.ToUpper(text[i]) : char.ToLower(text[i]));

        return result.ToString();
    }
}
Console.WriteLine("Prącie (łac. penis a. membrum virile) jest narządem homologicznym żeńskiej łechtaczki.".Pokemonize());
1
penis napisał(a):
if (string.IsNullOrEmpty(input)) return null;

Operacja np. na ciągu spacji lub pustym łańcuchu na pewno nie powinna dawać w wyniku null.

penis napisał(a):
output += input[i].ToString().ToUpper();

Zamieniać char na string tylko po to, aby zrobić ToUpper()? Fuj.

1 użytkowników online, w tym zalogowanych: 0, gości: 1