Concordance


General Statement: For a given text file, generate a list of words in alphabetical order that are in the text file and the lines on which those words occur.

Input: The first line of the data set for this problem is an integer that represents the number of lines that follow.

Output: Output the words in alphabetical order. After each word write the line number of each occurrence of that word. Start the number lists in the same column.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
The maximum line length is 40.
The maximum number of lines is 6.

Sample Input:
3
DOO WOP DOO WOP DOO WOP
SHOOBY DOOBY DOO
WOO WOO

Sample Output:
DOO 1 1 1 2
DOOBY 2
SHOOBY 2
WOO 3 3
WOP 1 1 1

Solutions :

#include <stdio.h>
#include <string.h>

int main ()
{
    int lines;
    scanf ("%d", &lines);
    getchar ();

    char input [6] [42];
    char words [42] [42];
    char temp [42];
    int count_word = 0;

    for ( int i = 0; i < lines; i++ ) {
        gets (input [i]);
        strcpy (temp, input [i]);

        char *token = strtok (temp, " ");

        while ( token ) {
            strcpy (words [count_word++], token);
            token = strtok (NULL, " ");
        }
    }

    for ( int i = 0; i < count_word; i++ ) {
        for ( int j = i + 1; j < count_word; j++ ) {
            if ( strcmp (words [i], words [j]) > 0 ) {
                strcpy (temp, words [i]);
                strcpy (words [i], words [j]);
                strcpy (words [j], temp);
            }
        }
    }

    for ( int i = 0; i < count_word; i++ ) {
        if ( strcmp (temp, words [i]) != 0 ) {
            printf ("%s\t", words [i]);

            for ( int j = 0; j < lines; j++ ) {
                strcpy (temp, input [j]);
                char *token = strtok (temp, " ");

                while ( token ) {
                    if ( strcmp (token, words [i]) == 0 )
                        printf (" %d", j + 1);
                    token = strtok (NULL, " ");
                }
            }

            strcpy (temp, words [i]);
            printf ("\n");
        }
    }

    return 0;
}
Advertisements

One thought on “Concordance

  1. #include
    #include
    #include
    #include

    using namespace std;

    int main()
    {
    int n,i=0,k=0,map_size;

    const char * sp=” “;
    string str2=”ok”;
    map m,ma;

    cin>>n>>ws;
    map_size=n;
    while(n–)
    {
    string str;
    getline(cin,str);

    m[i]=str;

    char * cstr = new char [str.length()+1];

    std::strcpy (cstr, str.c_str());

    char *pch=strtok(cstr,” “);

    while(pch !=NULL)
    {
    ma[k]=pch;
    //char * cs = pch;
    str2=ma[k];
    str2.insert(str2.length(),” “);
    ma[k]=str2;
    k++;
    pch=strtok(NULL,” “);
    //cout<<"\n strtok="<<k<<endl;
    }
    i++;
    }

    for(int i=0;i<k; i++)
    {
    for(int j=i+1;jma[j])
    swap(ma[i],ma[j]);
    }
    }

    cout<<ma[0];
    for(i=0;i<k;i++)
    {
    size_t found=-1;
    for(int j=0;j<map_size;j++)
    {
    str2=m[j];
    str2.insert(str2.length()," ");
    found=str2.find(ma[i],found+1);

    if (found!=string::npos)
    {
    if( ma[i].length() !=0){
    //cout<<"i="<<i<<" ma="<<ma[i]<<" found at "<<j+1<<" line at "<<found<< " ma length="<<ma[i].length()<<endl;

    cout<<" "<<j+1;
    if(ma[i]!=ma[i+1])
    {
    found=string::npos;
    cout<<endl<<ma[i+1];
    }
    i++;
    j–;

    }
    }
    }
    i–;
    //cout<<"\ni="<<i<<" ma@i="<<ma[i]<<"e"<<endl;
    }

    return 0;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s