Дана строка, заменить 'a' на 'b', а 'b' на 'a' - C#
Формулировка задачи:
Здравствуйте, помогите, пожалуйста, найти ошибки. Задание:Дана строка, заменить 'a' на 'b', а 'b' на 'a'.
Листинг программы
- using System;
- namespace Prog1
- {
- class Program
- {
- static void Main()
- {
- Console.WriteLine("Введите строку :");
- string s = Console.ReadLine();
- for (int i = 0; i < s.Length; i++)
- {
- if (s[i] == 'a')
- {
- s = s.Replace("a", "b");
- }
- if (s[i] == 'b')
- {
- s = s.Replace("b", "a");
- }
- }
- Console.WriteLine(s);
- Console.ReadKey();
- }
- }
- }
Решение задачи: «Дана строка, заменить 'a' на 'b', а 'b' на 'a'»
textual
Листинг программы
- using System;
- namespace OtherConsoleExperiments
- {
- class Program
- {
- static void Main(string[] args)
- {
- string source = "Test string: aabbccbbaa";
- char[] destChar = new char[source.Length];
- int destIndex = 0;
- foreach (char c in source)
- {
- destChar[destIndex++] = c == 'a' ? 'b' : (c == 'b' ? 'a' : c);
- }
- string destString = new string(destChar);
- Console.WriteLine(destString);
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д