write a C program : To find out the number of identical
words in two files . the file name should be taken as
command line argument .



write a C program : To find out the number of identical words in two files . the file name should b..

Answer / mohammed asif

//Email: mohdasif_2688@rocketmail.com
//Hello friends This program sure contains some errors which need to be rectified
//If any problem feel free to contact me at my email address i am a fresher (2012 passed out) too looking for a job.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#include<ctype.h>

int main(int argc,char *argv[])
{
char word[15]="\0",ch='\0',s[80]="\0";
FILE *fp,*fp1;
int init,count=0,l,i=0,j=0,k1;

fp=fopen("src.txt","r");
if(fp==NULL)
{
printf("Invalid File name");
exit(0);
}


while(ch!=-1)
{
init=0;
strset(word,'\0');
while((ch=fgetc(fp))!=' ')
{
if(ch!=-1)
{
if(ch!='\t')
{
if(ch!='\n')
{
word[init]=ch;
init++;
}
else
break;
}
else
break;
}
else
break;

}
word[init]='\0';
fp1=fopen("dest.txt","r");
if(fp1==NULL)
{
printf("Invalid file name");
exit(0);
}
if(fgets(s,79,fp1)!=NULL)
{
//Variables for the logic initializing
i=0;
j=0;k1=0;

//Possible ERROR Location
//here you get two array of chars word[] and s[]
//If finding error then work on these arrays to just find the substring in the main s[] string

l=strlen(word);
while (word[i]!=EOF)
{
if (s[i]==word[j])
{
i++;
j++;
k1=1;
if (j==l)
{
j=0;
count++;
}
}
else
{
if (k1==1)
{
j=0;
k1=0;
}
else
i++;
}
}

// end of Possible Error Location

}

fclose(fp1);

}
printf("\nNumber of Identical words is : %d",count);
fclose(fp);
system("pause");
}


//Note: This program does not take arguments from CMD. If you wish to then just use
// argv[1] in place of "src.txt" : i.e source file
// argv[2] in place of "dest.txt" : i.e destination file
//argc[1] & argv[2] are two text files with multiple words in the solutions directory
//Program written using MSVisual c++

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

What is the role of && operator in a program code?

0 Answers  


Do variables need to be initialized?

0 Answers  


what is the maximum no. of bytes calloc can allocate

4 Answers   Mphasis,


How do I declare a pointer to an array?

6 Answers   IBM,


the factorial of non-negative integer n is written n! and is defined as follows: n!=n*(n-1)*(n-2)........1(for values of n greater than or equal to 1 and n!=1(for n=0) Perform the following 1.write a c program that reads a non-negative integer and computes and prints its factorial. 2. write a C program that estimates the value of the mathematical constant e by using the formula: e=1+1/!+1/2!+1/3!+.... 3. write a c program the computes the value ex by using the formula ex=1+x/1!+xsquare/2!+xcube/3!+....

2 Answers   Ignou,






What does dm mean sexually?

0 Answers  


What are header files why are they important?

0 Answers  


Create a registration form application by taking the details like username, address, phone number, email with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 3 users and display the details. While taking input password must appear as “****”.

0 Answers  


What are variables and it what way is it different from constants?

0 Answers  


What is the Purpose of 'extern' keyword in a function declaration?

0 Answers  


How to implement variable argument functions ?

1 Answers   HP,


How can a number be converted to a string?

1 Answers  


Categories