GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 405 of 453    Print  
Write a program to find distinct word from a file
Write a program to find distinct word from file,and to check a particular word present or not in each line


  
Total Answers and Comments: 3 Last Update: February 20, 2008     Asked by: bismitapadhy 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: stevenong2007
 
//
// File:   FindWord3.cc
// Author: Steven Ong
//
// Purpose:
/**
 * I used this program as a 'try out' on Netbeans 6.0
 * with Cygwin (GCC)3.4.4 on Win2k
 * Aso to review the pointer operation in C
 *
 */
//

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

//
//
//

int main(int argc, char** argv) {
    FILE *inFile;
    char c, buff[120], *t;
    int line = 1;
    
    if (argc != 3) {
        perror("Usage: findwordinfile [word] [fileName] n");
        exit(1);
    }
    
    inFile = fopen(argv[2], "r");
    printf("argv[1] = [%s]n", argv[1]);
    
    if(inFile == NULL){
        perror("File cannot be opened!");
        exit(1);
    }
    
    //reset the pointer to point to the begining of buff
    t = buff;
    do {
        c = fgetc(inFile);
        
        // This necessary for it to work on MS-Windows
        // In Windows, the newline goes in pair "rn"
        // Pain! Pain! Pain!
        if (c == 'r'){
            continue;
        }
        if (c == 'n'){
           
            if (strcmp(buff, argv[1])== 0){
                printf("found [%s] at line %dn", buff, line);
            }
            line++;
            
            //Reset the buffer
            memset(buff, '', sizeof(buff));
            
            //reset the pointer to point to the begining of buff
            t = buff;
            continue;
        }
        if (c == ' '){
            
            if (strcmp(buff, argv[1])== 0){
                printf("found [%s] at line %dn", argv[1], line);
            }
            
            //Reset the buffer
            memset(buff, '', sizeof(buff));
            
            //reset the pointer to point to the begining of buff
            t = buff;
            continue;
        }
        else {
            // This is not a white space character
            // we need to collect it. Post increment the pointer
            // to the next index of the buffer
            *(t++) = c;
        }
        //printf("buff = [%s] n", buff);   
        
    }while(c != EOF);   
    
    return (EXIT_SUCCESS);
}



I used the following text file to test it:

    This the test
    The test is good
    so far so good

...e.g.,

$ ./findwordinfile test ./test.txt
argv[1] = [test]
found [test] at line 1
found [test] at line 2






Above answer was rated as good by the following members:
yogaa_mani
February 18, 2008 00:16:54   #1  
stevenong2007 Member Since: February 2008   Contribution: 3    

RE: Write a program to find distinct word from a file
//
// File: FindWord3.cc
// Author: Steven Ong
//
// Purpose:
/**
* I used this program as a 'try out' on Netbeans 6.0
* with Cygwin (GCC)3.4.4 on Win2k
* Aso to review the pointer operation in C
*
*/
//

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

//
//
//

int main(int argc char** argv) {
FILE *inFile;
char c buff[120] *t;
int line 1;

if (argc ! 3) {
perror("Usage: findwordinfile [word] [fileName] n");
exit(1);
}

inFile fopen(argv[2] "r");
printf("argv[1] [ s]n" argv[1]);

if(inFile NULL){
perror("File cannot be opened!");
exit(1);
}

//reset the pointer to point to the begining of buff
t buff;
do {
c fgetc(inFile);

// This necessary for it to work on MS-Windows
// In Windows the newline goes in pair "rn"
// Pain! Pain! Pain!
if (c 'r'){
continue;
}
if (c 'n'){

if (strcmp(buff argv[1]) 0){
printf("found [ s] at line dn" buff line);
}
line++;

//Reset the buffer
memset(buff ' ' sizeof(buff));

//reset the pointer to point to the begining of buff
t buff;
continue;
}
if (c ' '){

if (strcmp(buff argv[1]) 0){
printf("found [ s] at line dn" argv[1] line);
}

//Reset the buffer
memset(buff ' ' sizeof(buff));

//reset the pointer to point to the begining of buff
t buff;
continue;
}
else {
// This is not a white space character
// we need to collect it. Post increment the pointer
// to the next index of the buffer
*(t++) c;
}
//printf("buff [ s] n" buff);

}while(c ! EOF);

return (EXIT_SUCCESS);
}



I used the following text file to test it:

This the test
The test is good
so far so good

...e.g.

$ ./findwordinfile test ./test.txt
argv[1] [test]
found [test] at line 1
found [test] at line 2





 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 19, 2008 10:16:55   #2  
stevenong2007 Member Since: February 2008   Contribution: 3    

RE: Write a program to find distinct word from a file
The statements:

if(c 'r') ... should be .... if(c 'r')

...And

if(c 'n') ... should be .... if(c 'n')

For some reason it took out the slashes when I cut and paste the program in to the text editor. But you should got the idea of my logic.

Enjoy
Steve

 
Is this answer useful? Yes | No
February 20, 2008 01:04:43   #3  
stevenong2007 Member Since: February 2008   Contribution: 3    

RE: Write a program to find distinct word from a file
Here is how I would write this in C++.....Enjoy

//
// File: FindWordCPP.cc
// Author: Steven Ong
//
// Created on February 19 2008 11:05 PM
//

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>

using namespace std;
//
//
//
int main(int argc char** argv) {

if (argc ! 3) {
cerr << "Usage: FindWordCPP [word] [fileName]" << endl;
exit(1);
}

ifstream inFile(argv[2]);
if(!inFile){
cerr <<"File cannot be opened!" << endl;
exit(1);
}

//cout << "argv[1] " << argv[1] << endl;
string buff tmp;
int line 1;
while(getline(inFile buff)){
//if(buff.find(argv[1]) ! string::npos)
//cout << "found [" << argv[1] << "] at line " << line << endl;
string delimiters " \r\n";
int end 0 start buff.find_first_not_of(delimiters);

while (start ! string::npos){
end buff.find_first_of(delimiters start + 1);
if(end string::npos){
end buff.length();
}
tmp buff.substr(start end - start);
//cout << "tmp |" << tmp << "|" << endl;

if (tmp.compare(argv[1]) 0) {
cout << "found [" << argv[1] << "] at line " << line << endl;
}
start buff.find_first_not_of(delimiters end + 1);
}
line++;
}
//cout << buff << endl;

return (EXIT_SUCCESS);
}




 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape