Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I have a text file containing:

FIRST_WORD
The boy
The man
The girl

SECOND_WORD
is reading
is writing

THIRD_WORD
a book
a novel

I require the following output - (every conjugation of the 3 groups of words):

The boy is reading a book
The boy is reading a novel
The boy is writing a book
The boy is writing a novel
The man is reading a book

Any help would be greatly appreciated.

Thanks

What I have tried:

I understand a loop is required to parse each lesson but I not sure how to group together the words to form complete sentences.
Posted
Updated 23-Jan-17 22:41pm

The first thing you need to do is read the file, and then locate the groups.
Reading it is easy:
C#
string[] lines = File.ReadAllLines(pathToFile);
That will give you an array of strings, each of which contain a line from the file.
Locating the groups is more complicated, but the way I'd do it - assuming the "group headings" are fixed - would be to set up an array of strings to contain the headings:
C#
string[] groupHeadings = new string[] { "FIRST_WORD", "SECOND_WORD", "THIRD_WORD" };
And you will need somewhere to keep the groups:
C#
List<string>[] groups = new List<string>[groupHeadings.Length];
for (int i = 0; i < groupHeadings.Length; i++)
    {
    groups[i] = new List<string>();
    }
This creates an array of lists: one list for each group you expect from the file.
Now, you can look at each line in the file you read, and separate it into groups.
C#
int currentGroup = -1;
foreach (string line in lines)
    {
    if (!string.IsNullOrWhiteSpace(line))
        {
        if (groupHeadings.Contains(line))
            {
            currentGroup = Array.IndexOf(groupHeadings, line);
            }
        else
            {
            if (currentGroup >= 0 && currentGroup < groups.Length)
                {
                groups[currentGroup].Add(line);
                }
            else
                {
                string err = string.Format("Data cannot be added to a group: Group number {0}, line = \"{1}\"", currentGroup, line);
                throw new ApplicationException(err);
                }
            }
        }
    }
After that, the combinations are up to you!
 
Share this answer
 
Comments
Sanxion87 24-Jan-17 4:31am    
Thank-you very much...I will give it a try!
OriginalGriff 24-Jan-17 4:33am    
You're welcome!
Quote:
but I not sure how to group together the words to form complete sentences.
For the output, you need to search for nested loops.
Look at your output, each sentence put together 1 piece of each category and enumerate each possibility. Sentences are made in this order 111, 112, 121, 122, 211, 212, 221 ...


We do not do your HomeWork, so no code.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900