C# :: Aufgabe #27

11 Lösungen Lösungen öffentlich

Text abwechselnd in Groß- und Kleinschreibung

Anfänger - C# von Dome - 29.12.2012 um 01:34 Uhr
Schreiben Sie ein Programm, welches einen eingegeben Text so manipuliert, das der Text abwechselnd in Groß- und Kleinschreibung auf den Bildschirm ausgegeben wird.

Konsolenausgabe:

Texteingabe: Beispieltext
Textausgabe: BeIsPiElTeXt

Lösungen:

vote_ok
von pocki (4190 Punkte) - 29.12.2012 um 20:31 Uhr
Kurz und knapp:
Quellcode ausblenden C#-Code
void main()
{
	Console.Write("Texteingabe: ");
	string input = Console.ReadLine();
	char[] inputChars = input.ToCharArray();
	
	string output = string.Empty;
	for (int i = 0; i < inputChars.Length; i++)
	{
		output += ((i % 2 == 0) ? inputChars[i].ToString().ToUpper() : inputChars[i].ToString().ToLower());
	}
	Console.WriteLine("Textausgabe: {0}", output);	
}
vote_ok
von Process1 (1180 Punkte) - 10.01.2013 um 02:31 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Caesar {
    class Program {
        static void Main(string[] args) {
            Console.Write("Texteingabe: ");
            string text = Console.ReadLine();
            string s = "";
            string[] tmp = new string[text.Length]; 
            for(int i = 0, j = 0; i < text.Length; i++)
            {
                tmp[i] = Convert.ToString(text[i]);
                if(j == 0)
                {
                    tmp[i] = tmp[i].ToUpper();
                    j = 1;
                }
                else
                {
                     tmp[i] = tmp[i].ToLower();
                    j = 0;
                }
                s += tmp[i];
            }
            Console.Write("Textausgabe: " + s);
        }
    }
}

vote_ok
von B.Denger (730 Punkte) - 06.09.2013 um 11:51 Uhr
Quellcode ausblenden C#-Code
static void Main(string[] args)
        {
            Console.Write("Texteingabe: ");
            string input = Console.ReadLine();
            input = input.Trim();
            int length = input.Length;
            char[] arr = new char[length];
            int i = 0;
            foreach (char s in input)
            {
                if (i % 2 == 0)
                {
                    arr[i] = char.ToUpper(s);
                }
                else
                {
                    arr[i] = s; 
                }
                i++;
            }
            Console.Write("Textausgabe: ");
            Console.Write(arr);

                Console.ReadLine();
        }
vote_ok
von wladi-g (1310 Punkte) - 02.06.2014 um 16:38 Uhr
Quellcode ausblenden C#-Code
using System;

namespace GroßKleinAbwechslung
{
    class Program
    {
        static void Main(string[] args)
        {
            char temp;
            string text = "";
            System.Console.Write("Texteingabe: ");
            text = System.Console.ReadLine();
            for (int i = 0; i < text.Length; i++)
            {
                if (i % 2 == 0)
                {
                    temp = Char.ToUpperInvariant(text[i]);
                    text = text.Remove(i, 1);
                    text = text.Insert(i, temp.ToString());
                }
                else
                {
                    temp = Char.ToLowerInvariant(text[i]);
                    text = text.Remove(i, 1);
                    text = text.Insert(i, temp.ToString());
                }
            }
            System.Console.WriteLine("Textausgabe: {0}", text);
        }
    }
}
vote_ok
von hobeditz (650 Punkte) - 15.09.2014 um 19:17 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GroßKlein
{
    class Program
    {
        static void Main(string[] args)
        {
            String eingabe;
            Char[] zeichen;

            Console.Write("Texteingabe: ");
            eingabe = Console.ReadLine();

            Console.Write("Textausgabe: ");

            zeichen = eingabe.ToLower().ToCharArray(); //Erstmal wird der eingegebene Text in Kleinbuchstaben umgewandelt und die Zeichen einzeln gespeichert.

            //Jedes Zeichen wird geprüft. Da ein Index mit 0 beginnt wird diese Variable auf diesen Wert gesetzt.
            for(int index = 0; index < zeichen.Length; index++)
            {
                if (index % 2 == 0) //Falls der Rest bei index / 2 gleich 0 ist... ( /2 -> Weil jede zweite Zahl durch 2 teilbar ist)
                {
                    zeichen[index] = Char.ToUpper(zeichen[index]); //... wird das jeweilige Zeichen in einen Großbuchstaben geändert...
                }

                Console.Write(zeichen[index]); //... und anschließend ausgegeben. (Ansonsten bleibt es klein);
            }

            Console.ReadLine();

        }
    }
}


Dieser Code ergibt dann:

Konsolenausgabe:


Texteingabe: Beispieltext
Textausgabe: BeIsPiElTeXt


Es geht aber auch:

Konsolenausgabe:


Texteingabe: BEISPIELTEXT
Textausgabe: BeIsPiElTeXt
vote_ok
von DBqFetti (2480 Punkte) - 15.03.2015 um 10:13 Uhr
Quellcode ausblenden C#-Code
using System;

namespace Abwechselnd_GroßKlein {
	class Program {
		static void Main() {

			Console.Write("Texteingabe>");
			char[] text = Console.ReadLine().ToCharArray();
			
			for (int i = 0; i < text.Length; i++) {
				if ((i & 1) == 1)
					text[i] = Convert.ToChar(text[i].ToString().ToLower());
				else
					text[i] = Convert.ToChar(text[i].ToString().ToUpper());
			}

			Console.WriteLine(text);

			Console.ReadKey(true);
		}
	}
}

vote_ok
von niknik (1230 Punkte) - 07.08.2015 um 12:54 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrossKlein
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Schreibe einen Text yoa");
            string eingabe = Console.ReadLine();

            StringBuilder strBuilder = new StringBuilder(eingabe);


            for (int i = 0; i < eingabe.Length; i++)
            {
                if (i % 2 == 0)
                {
                    strBuilder[i] = Char.ToUpper(eingabe[i]);
                }
                else
                {
                    strBuilder[i] = Char.ToLower(eingabe[i]);
                }
            }
            eingabe = strBuilder.ToString();
            Console.WriteLine(eingabe);
            Console.ReadKey();
        }
    }
}
vote_ok
von bubetin (100 Punkte) - 11.02.2016 um 15:56 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class Program
{
    static void Main()
    {
        char[] input = Console.ReadLine().ToCharArray();
        for (int i = 0; i < input.Length; i++)
        {
            if (i % 2 == 0)
            {
                Console.Write(Char.ToUpper(input[i]));
            }
            else
            {
                Console.Write(Char.ToLower(input[i]));
            }
        }
      

    }
}
vote_ok
von kjaenke (1140 Punkte) - 04.01.2018 um 10:18 Uhr
Quellcode ausblenden C#-Code
namespace Exercise_27
{
    using System;

    internal static class Program
    {
        private static void Main()
        {
            Console.Write("Texteingabe: ");
            var input = Console.ReadLine();
            var output = string.Empty;
            var isBig = true;
            if (input != null)
            {
                foreach (var t in input)
                {
                    if (isBig)
                    {
                        output += t.ToString().ToUpper();
                        isBig = false;
                    }
                    else
                    {
                        output += t.ToString().ToLower();
                        isBig = true;
                    }
                }
            }

            Console.Write($"Textausgabe: {output}");

            Console.ReadLine();
        }
    }
}
vote_ok
von stbehl (1640 Punkte) - 05.02.2018 um 09:41 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrainYourProgrammer27
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Texteingabe: ");
            string text = Console.ReadLine(), neuerText = "";
            for (int i = 0; i < text.Length; i++)
            {
                if(i%2 == 0)
                {
                    neuerText += Char.ToUpper(text[i]);
                }
                else
                {
                    neuerText += Char.ToLower(text[i]);
                }
            }
            Console.WriteLine("Textausgabe: " + neuerText);
            Console.ReadKey();
        }
    }
}
vote_ok
von stcalvin (970 Punkte) - 05.02.2018 um 13:57 Uhr
Quellcode ausblenden C#-Code
        static void Aufgabe_27()
        {
            string eingabe, ausgabe = "";

            Console.Write("Texteingabe: ");
            eingabe = Console.ReadLine();

            for (int i = 0; i <= eingabe.Length - 1; i++)
            {
                if (i % 2 == 0)
                {
                    ausgabe += eingabe[i].ToString().ToLower();
                }
                else
                {
                    ausgabe += eingabe[i].ToString().ToUpper();
                }

            }

            Console.Write("Textausgabe: {0}", ausgabe);
        }
2113063

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.