My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
October 13th, 2015, 10: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.

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

FILE*readfile;

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: ");
	gets(temp_1);
	strcpy(searchword_1, strlwr(temp_1));
	
	printf("Enter search word 2: ");
	gets(temp_2);
	strcpy(searchword_2, strlwr(temp_2));
	
	printf("Enter filename: ");
	gets(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){
			count_1++;
			printf("\n%s", line);
			break;
		}
		
		if (strstr(case_converted_line, searchword_2) != NULL){
			count_2++;
			printf("\n%s", line);
			break;
		}
	}

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

Posts: 2,132
Thanks: 717

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  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
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 06:25 AM
Book search Mark_sr Math Books 3 October 16th, 2012 08:05 AM
search index sigma123 Linear Algebra 0 August 7th, 2012 02:53 AM
Linear Programming Word Problems jakej78 Algebra 1 December 16th, 2009 09:03 PM
search for a certain proof natus zeri Real Analysis 1 December 19th, 2007 11:17 PM





Copyright © 2018 My Math Forum. All rights reserved.