The Student Room Group

Computer Science Programming Task

Hi, any help would be appreciated :smile:) (C#)

Write a program that gets two words from the user and then displays a message saying if the first word can be created using the letters from the second word or not.

For example:
The word EAT can be formed from the word ATE as the first word uses one E, one A and one T and the second word also contains one of each of these letters.
The word EAT can be formed from the word HEART as the second word contains one E, one A and one T which are the letters needed to form the first word.
The word TO can be formed from the word POSITION as the second word contains one T and (at least) one 0 which are the letters needed to form the first word.
The word MEET cannot be formed from the word MEAT as the second word only contains one E and two Es are needed to form the first word.

You may assume that the user will only enter words that consist of upper case letters.

Thanks again
(edited 3 years ago)
Reply 1
Yh same I need an answer for this ASAP a levels coming soon
Reply 2
same I am very confused right now :frown:
Original post by Lazmiin
same I am very confused right now :frown:


Is it the exact same task? How far have you gotten with it currently? :smile:

public Dictionary<char, int> LetterCount(string word) {
Dictionary<char, int> letterCount = new Dictionary<char, int>();
char[] wordLetters = word.ToCharArray();

for (int i = 0; i < wordLetters.Length; i++) {
if (letterCount.ContainsKey(wordLetters)) {
letterCount[wordLetters]++;
}
else {
letterCount.Add(wordLetters, 1);
}
}

return letterCount;
}


Here’s a start for you.
(edited 2 years ago)
Reply 5
The problem can be simplified as:
Is word 2 a subset of word 1?

(Where word 1 is technically speaking a multiset, since we care about the number of letters involved.)

A simple way to do this is to make a list (multiset) of the allowed letters from word 1, then make sure each letter of word 2 is in that list of allowed letters, removing used letters as we go.

Here is a solution:
https://dotnetfiddle.net/AXnHx8
Reply 6
Original post by username5723140
Hi, any help would be appreciated :smile:) (C#)

Write a program that gets two words from the user and then displays a message saying if the first word can be created using the letters from the second word or not.

For example:
The word EAT can be formed from the word ATE as the first word uses one E, one A and one T and the second word also contains one of each of these letters.
The word EAT can be formed from the word HEART as the second word contains one E, one A and one T which are the letters needed to form the first word.
The word TO can be formed from the word POSITION as the second word contains one T and (at least) one 0 which are the letters needed to form the first word.
The word MEET cannot be formed from the word MEAT as the second word only contains one E and two Es are needed to form the first word.

You may assume that the user will only enter words that consist of upper case letters.

Thanks again

using System;

namespace Activity5
{
class Program
{
static void Main(string[] args)
{
bool CanBeMAdeFromWord = true;
int Loc = 0;

Console.WriteLine("Enter your first word in UPPERCASE: ");
string Str1 = Console.ReadLine();

Console.WriteLine("Enter your second word in UPPERCASE: ");
string Str2 = Console.ReadLine();

for (int i = 0; i < Str1.Length; i++)
{
if(Str2.Contains(Str1))
{
Loc = Str2.IndexOf(Str1);
Str2 = Str2.Remove(Loc, 1);
}
else
{
CanBeMAdeFromWord = false;
}
}

if(CanBeMAdeFromWord)
{
Console.WriteLine("The First word can be formed from the second word.");
}
else
{
Console.WriteLine("The first word cannot be made from the sercond word.");
}

}
}
}

Quick Reply

Latest

Trending

Trending