C Program to search a word in a given file and display all its position

Write a program to find a word in a given file and display all it’s positions in C.

Example,

Input Text File
Hello My name is John Smith
John is a Good man. John is very Tall.
Please stop talking John
Go to principle office John Smith

Word to Search
John

Output
Line: 0 Column: 17
Line: 1 Column: 0
Line: 1 Column: 20
Line: 2 Column: 20
Line: 3 Column: 23

Prerequisite Knowledge

In short, the FILE object basically allows us to access and control file information. fgets() is used to read the file line by line.

Steps

  1. Use FILE object to access/open the file you want to search.
  2. Let wrd be the word you want to search.
  3. Initialize line = 0. line is used to track the line number.
  4. Use fgets() to read the target file line by line. Store each line in a variable called buffer. Perform the following operations for each line.
    1. Read the content of the buffer word by word. Compare each word of buffer to wrd. If they match, then print line (line number) and index of the word.
    2. Increment line and move to the next line.
  5. Close the file.

Program to find a word in a file and display all its position

text.txt

Hello My name is John Smith
John is a Good man. John is very Tall.
Please stop talking John
Go to principle office John Smith
#include <stdio.h>
#include <string.h>

int main() {

	char wrd[256], buffer[256];
	int n, m, i, j, line;
	
	FILE* fp;
	fp = fopen("text.txt", "r");	// open file

	printf("Enter the word you want to search in the file: ");
	gets(wrd);

	m = strlen(wrd); // length of input word

	printf("All positions of word \"%s\" in the file\n", wrd);

	line = 0;
	// the following loop the file fp line by line
	// each line is stored in buffer
	while (fgets(buffer, 256, fp) != NULL) {

		i = 0;
		n = strlen(buffer);
		// the followinf loop find position of the input word in the current line and
		// print the position of the word on the screen
		// the loop basically reads each word of the file and compare it with the input word
		while (i < n) {

			// comparing current word with input word
			j = 0;
			while (i < n && j < m && buffer[i] == wrd[j]) {
				++i, ++j;
			}

			// the following condition implies that the current word of buffer
			// is equal to input word
			if ((i == n || buffer[i] == ' ' || buffer[i] == '\n') && j == m) {
				printf("Line: %d ", line);
				printf("Column: %d\n", i - m);
			}

			// moving to next word
			while (i < n && buffer[i] != ' ') {
				++i;
			}
			++i;

		}

		++line;

	}

	fclose(fp);

}

Output

Enter the word you want to search in the file: John
All positions of word "John" in the file
Line: 0 Column: 17
Line: 1 Column: 0
Line: 1 Column: 20
Line: 2 Column: 20
Line: 3 Column: 23

3 thoughts on “C Program to search a word in a given file and display all its position”

    1. If you want to display the whole line then just add printf(“Line: %s”, buffer); inside the If block in which you are printing Line and column number.

Leave a Comment

Your email address will not be published. Required fields are marked *