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

Questions by bismitapadhy   answers by bismitapadhy

Showing Answers 1 - 9 of 9 Answers

//
// 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




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

  Was this answer useful?  Yes

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



  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions