My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum

LinkBack Thread Tools Display Modes
October 13th, 2015, 09:53 PM   #1
Senior Member
hyperbola's Avatar
Joined: Dec 2014
From: The Asymptote

Posts: 142
Thanks: 6

Math Focus: Certainty
C Programming - Word Search Program bug

The program is asks the user to read in two search words.
The program asks the user to read in a file (txt).
The program then counts the occurrence of each search word in the file.

When the user reads in a search word, that search word is stored in $\displaystyle temp$. I then use $\displaystyle strcpy(searchword, strlwr(temp))$ to copy the lower case converted search word into $\displaystyle searchword$.

When I read the file, I convert the entire contents to lower case. I then use $\displaystyle strstr$ and $\displaystyle count++$to determine the occurrence of the search word.

The idea of converting both the search word and file contents to lower case is I'm making the word search 'case-insensitive' ('THE' is the same as 'the').

The problem I'm having is that when I read in a word, example 'jack', I know the file contains 'Jack' but the $\displaystyle strstr$ does not identify them as being the same.

With the case conversions performed, shouldn't $\displaystyle strstr$ identify them as being both the same??

Need help. Thanks.

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


char filename[50];
char searchword_1[50], searchword_2[50];
char SEARCHWORD_1[50], SEARCHWORD_2[50];
char temp_1[50], temp_2[50];
char case_converted_line[1000];
char line[1000];
int i, line_count, count_1, count_2;

int main() {
	printf("Enter search word 1: ");
	strcpy(searchword_1, strlwr(temp_1));
	printf("Enter search word 2: ");
	strcpy(searchword_2, strlwr(temp_2));
	printf("Enter filename: ");
	readfile = fopen(filename, "r");
	while (fgets(line, 1000, readfile) != NULL){
		strcpy(case_converted_line, strlwr(line));
		if (strstr(case_converted_line, searchword_1) != NULL){
			printf("\n%s", line);
		if (strstr(case_converted_line, searchword_2) != NULL){
			printf("\n%s", line);

	printf("%d", count_1);
	printf("%d", count_2);
hyperbola is offline  
October 30th, 2015, 06:12 AM   #2
Senior Member
Joined: Apr 2014
From: Glasgow

Posts: 2,155
Thanks: 731

Math Focus: Physics, mathematical modelling, numerical and computational solutions
One possibility is that you have white-space in one string but not the other. e.g. "Jack" and "Jack " will not match. Have you checked that?
Benit13 is offline  

  My Math Forum > Science Forums > Computer Science

bug, program, programming, search, word

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Average search cost of an unsuccessful search in a BST? flexdec Applied Math 0 July 16th, 2013 05:25 AM
Book search Mark_sr Math Books 3 October 16th, 2012 07:05 AM
search index sigma123 Linear Algebra 0 August 7th, 2012 01:53 AM
Linear Programming Word Problems jakej78 Algebra 1 December 16th, 2009 08:03 PM
search for a certain proof natus zeri Real Analysis 1 December 19th, 2007 10:17 PM

Copyright © 2019 My Math Forum. All rights reserved.