0% found this document useful (0 votes)
4 views2 pages

Match

The document contains a C function that searches for occurrences of a specified word in a text file, ignoring case differences. It reads the file character by character, builds words, and compares them to the target word, storing the positions of matches. The function returns an array containing the count of matches and their respective positions in the file.

Uploaded by

babbba48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Match

The document contains a C function that searches for occurrences of a specified word in a text file, ignoring case differences. It reads the file character by character, builds words, and compares them to the target word, storing the positions of matches. The function returns an array containing the count of matches and their respective positions in the file.

Uploaded by

babbba48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <string.h>
#include "filer.h"
#include <ctype.h>
#include <stdlib.h>

int* match(char *argv, char *word)


{
FILE *f = fopen(argv,"r");

// Declartion of important variables;


int count =0;
char *buffer = malloc(48);
int i;
static int match[48];

//convert the word to lower


for(i=0; i<strlen(word) ; i++)
{
match[i] = '\0';
if(isupper(word[i]))
word[i] = tolower(word[i]);
}

word[i] ='\0';
char c;

int position=0;

while((c=fgetc(f))!=EOF)
{
//cleaning up the buffer from garbage
for(int o=0; o<48 ; o++)
buffer[o] = '\0';

//building words

for(c,i=0; c!=' ' && c!='\n' &&c!=',' && c!= ';'; c=fgetc(f),i++)
{

if(i==0)
position = ftell(f);

if(i <47)
buffer[i] = c;

else
break;

}
//convert them to lower if they are upper
for(int k=0; k<strlen(buffer); k++)
{
if(isupper(buffer[k]))
buffer[k] = tolower(buffer[k]);
}
if(strlen(buffer)>0)
printf("%s,%s\n ",buffer,word);
//compare them
if(strcmp(word,buffer)==0)
{
count++;
match[count] = position;

//closing file and freeing memory (to avoid memory leak)


free(buffer);
fclose(f);
match[0] =count;

return match;
}

You might also like