Sunteți pe pagina 1din 5

10/23/2017 Concordance | Solved Programming Problems

Concordance

OCTOBER 3, 2009 / SHAHAB

i
Rate This

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 forma ed exactly like that for the sample output
given below.

Assumptions: All le ers are upper case.


The maximum line length is 40.
The maximum number of lines is 6.

Sample Input:
https://tausiq.wordpress.com/2009/10/03/concordance/ 1/5
10/23/2017 Concordance | Solved Programming Problems

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 :

1 #include <stdio.h>
2 #include <string.h>
3
4 int main ()
5 {
6 int lines;
7 scanf ("%d", &lines);
8 getchar ();
9
10 char input [6] [42];
11 char words [42] [42];
12 char temp [42];
13 int count_word = 0;
14
15 for ( int i = 0; i < lines; i++ ) {
16 gets (input [i]);
17 strcpy (temp, input [i]);
18
19 char *token = strtok (temp, " ");
20
21 while ( token ) {
22 strcpy (words [count_word++], token);
23 token = strtok (NULL, " ");
24 }
25 }
26
27 for ( int i = 0; i < count_word; i++ ) {
28 for ( int j = i + 1; j < count_word; j++ ) {
29 if ( strcmp (words [i], words [j]) > 0 ) {
30 strcpy (temp, words [i]);
31 strcpy (words [i], words [j]);
32 strcpy (words [j], temp);
33 }
34 }
35 }
36
37 for ( int i = 0; i < count_word; i++ ) {
38 if ( strcmp (temp, words [i]) != 0 ) {
39 printf ("%s\t", words [i]);
40
41 for ( int j
https://tausiq.wordpress.com/2009/10/03/concordance/ = 0; j < lines; j++ ) { 2/5
10/23/2017 Concordance | Solved Programming Problems

41 for ( int j = 0; j < lines; j++ ) {


42 strcpy (temp, input [j]);
43 char *token = strtok (temp, " ");
44
45 while ( token ) {
46 if ( strcmp (token, words [i]) == 0 )
47 printf (" %d", j + 1);
48 token = strtok (NULL, " ");
49 }
50 }
51
52 strcpy (temp, words [i]);
53 printf ("\n");
54 }
55 }
56
57 return 0;
58 }

Advertisements

Easy Problems

One thought on “Concordance”

1. Moktar Ali
MAY 18, 2014 AT 11:59 PM
#include
#include
#include
#include

using namespace std;

int main()
https://tausiq.wordpress.com/2009/10/03/concordance/ 3/5
10/23/2017 Concordance | Solved Programming Problems

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];
https://tausiq.wordpress.com/2009/10/03/concordance/ 4/5
10/23/2017 Concordance | Solved Programming Problems

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;
}

i
Rate This

https://tausiq.wordpress.com/2009/10/03/concordance/ 5/5

S-ar putea să vă placă și