Проверить, можно ли из букв, входящих в строку А, составить строку В - C#
Формулировка задачи:
Обязательно использовать String и StringBuilder.
Даны две строки А и B. Составьте программу, проверяющую, можно ли из букв, входящих в А, составить В.
Ошибку в том, что выхожу за диапазон в цикле, помогите!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LAB1__2_
{
class Class1
{
static void Main(string[] args)
{
Console.WriteLine("Введите строку А: ");
string A, s;
A = Console.ReadLine();
Console.WriteLine("Введите строку B: ");
StringBuilder B = new StringBuilder(Console.ReadLine());
for (int i = 0; i < B.Length; i++)
for (int j = 0; j < A.Length; j++)
{
if (B[i] == A[j])
{
A.Remove(j, 1);
B.Remove(i, 1);
}
}
s = B.ToString();
if (s == String.Empty)
Console.WriteLine("Можно!");
else
Console.WriteLine("Нельзя");
Console.ReadLine();
}
}
}Решение задачи: «Проверить, можно ли из букв, входящих в строку А, составить строку В»
textual
Листинг программы
Console.WriteLine("Введите строку А: ");
StringBuilder A = new StringBuilder(Console.ReadLine());
Console.WriteLine("Введите строку B: ");
string B = Console.ReadLine();
int index = 0;
for (int i = 0; i < B.Length; i++)
{
index = A.ToString().IndexOf(B[i]);
if (index >= 0)
A.Remove(index, 1);
else
break;
}
Console.WriteLine(index >= 0 ? "Можно!" : "Нельзя");
Console.ReadLine();