Complex String Manipulation program

Hi,
I need the help with the logic for below program -
there is a txt file containg some data on each line as follow:

First line: T (i.e.Number of test cases)
Secod line : M (i.e. Number of words or lines to follow ion each case)
Third onwards: String data in lower letters only..

Ex.
2
3
map
manip
hashcode
4
deltas
alphag
szeta
gamma
 
output:
false
true

like the txt file above.
Now the logic need to read the file and if the start letter of a word is end letter of some other word in the same test case.then the output true or else false.
Like in above example there are 2 test cases and 3 strings in 1st case, namely 'map','manip', and 'hashcode'.The last case has 4 strings.
If the words in each test case can be arranged like
deltas szeta alphag gamma
i.e. the start of each string is the end of some other and they can be arranged in the series like above!then its true otherwise false.
If can get the help for for both C++ and java it wil be nice

Thanks & regards,
LVL 2
Jack020800Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> If can get the help

What kind of help ?
0
a_bCommented:
good one
0
a_bCommented:
Here is the java code. Pls test and let me know.
package snippet;
 
 
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
 
/**
 * This program reads a text file line by line and print to the console. It uses FileOutputStream to read the file.
 */
public class FileInput
{
 
    public static void main (String[] args)
    {
 
        File file = new File("C:\\file.txt");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
 
        try
        {
            fis = new FileInputStream(file);
 
            // Here BufferedInputStream is added for fast reading.
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
 
            // dis.available() returns 0 if the file does not have more lines.
            int numTests = Integer.parseInt(dis.readLine());
            for (int i = 0; i < numTests; i++)
            {
                int tests = Integer.parseInt(dis.readLine());
                char[] firstLetters = new char[tests];
                char[] lastLetters = new char[tests];
                for (int j = 0; j < tests; j++)
                {
                    String word = dis.readLine();
                    firstLetters[j] = word.charAt(0);
                    lastLetters[j] = word.charAt(word.length() - 1);
                }
 
                System.out.println(processWords(firstLetters, lastLetters, tests));
            }
 
            // dispose all the resources after using them.
            fis.close();
            bis.close();
            dis.close();
 
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 
 
    /**
     * TODO: Documentation to be done for method 'processWords', by 'abha'..
     * 
     * @param anFirstLetters
     * @param anLastLetters
     * @param anTests
     */
    private static boolean processWords (char[] aFirstLetters, char[] aLastLetters, int numTests)
    {
        // Locate start point
        int startPoint = -1;
        int endPoint = -1;
        for (int i = 0; i < numTests; i++)
        {
            boolean flag = false;
            for (int j = 0; j < numTests; j++)
            {
                if (i != j && aFirstLetters[i] == aLastLetters[j])
                {
                    flag = true;
                    break;
                }
            }
            if (flag == false)
            {
                if (startPoint != -1)
                    return false;
                else
                    startPoint = i;
            }
 
        }
 
        char temp = aFirstLetters[0];
        aFirstLetters[0] = aFirstLetters[startPoint];
        aFirstLetters[startPoint] = temp;
 
        temp = aLastLetters[0];
        aLastLetters[0] = aLastLetters[startPoint];
        aLastLetters[startPoint] = temp;
 
 
        temp = aLastLetters[0];
        aLastLetters[0] = aLastLetters[startPoint];
        aLastLetters[startPoint] = temp;
 
        temp = aLastLetters[0];
        aLastLetters[0] = aLastLetters[startPoint];
        aLastLetters[startPoint] = temp;
 
 
        for (int i = 0; i < numTests - 1; i++)
        {
            boolean found = false;
            for (int j = i + 1; j < numTests; j++)
            {
 
                if (aLastLetters[i] == aFirstLetters[j])
                {
                    temp = aFirstLetters[i + 1];
                    aFirstLetters[i + 1] = aFirstLetters[j];
                    aFirstLetters[j] = temp;
 
                    temp = aLastLetters[i + 1];
                    aLastLetters[i + 1] = aLastLetters[j];
                    aLastLetters[j] = temp;
 
                    found = true;
                    break;
                }
 
            }
            if (found == false)
                return false;
 
 
        }
        return true;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

mrjoltcolaCommented:
Hi a_b, make sure to read the homework rules for Experts Exchange before posting complete code samples. This does appear to be a homework related question.
0
ozoCommented:
the code in http:#a24466919 causes an ArrayIndexOutOfBoundsException when given onput like
2
ab
ba
0
ozoCommented:
It also gives different answers for

4
za
ab
ac
ca

and

4
za
ac
ab
ca
0
Jack020800Author Commented:
Hi a_b,
love u so much for this nice code!Although i executed it just manually, I am happy.And for the helpfull comments from "ozo", I appreciate it and I will think further for the new version for the code to handle this bug!
For all those worrying for 'whether it is homework problem', so its not actually, I am IT professional.
Thanks for the comments guys.Now let me check the code at home and if enhancement is possible by me.

Regards,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.