1

writing another program, it reads a txt file, and stores all the letter characters and spaces (as \0) in a char array, and ignores everything else. this part works.

now what i need it to do is read a user inputted string, and search for that string in the array, then print the word every time it appears. im terrible at I/O in C, how do you read a string then find it in a char array?

3 Answers 3

2
#include <stdio.h>
...

char str [80];
printf ("Enter your word: ");
scanf ("%s",str);  

char* pch=strstr(fileData,str);
while (pch!=NULL)
{
 printf ("found at %d\n",pch-fileData+1);
 pch=strstr(pch+1,str);
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for being on the right track. The scanf's "%s" format is good if you want to consider the first space to be the end of the input string. If you want the whole line you'd need a "%[^\n]". Then, you should ensure no more than 80 characters are read by putting ala ("%.*s", sizeof str, str). And, you should use strstr() rather than strchr() to find the substrings.
@mikecavs Yes. That is char* with a content of your file. pInFile = fopen(infilename, "r"); fread(&fileData, BUFFER_SIZE, 1, pInFile);
@ErikW no - the format's not regular expressions. "%.*s" means to accept a maximum string-length argument followed by a pointer-to-string-buffer argument. The format's explained in the scanf docs.
0
  1. read in the user inputted string as a char array as well (cause strings are basically char* anyway in C)

  2. use a string matching algorithm like Boyer-Moore or Knutt-Morris-Pratt (more popularly known as KMP) - google for it if you like for C implementations of these - cause they're neat, tried and tested ways of searching strings for substrings and pattern matches and all.

  3. for each of these indexOf cases, print the position where the word is found maybe? or if you prefer, the number of occurrences.

Generally, the list of C string functions, found here, say, are of the format str* or strn*, depending on requirements.

2 Comments

1. is basically repeating his requirement. 2. is overkill for a beginner - what's wrong with strstr?
I'm repeating his second paragraph, after he said what he's already done, the part "now what i need to do is...", I did not repeat anything before that. Also, I was suggesting a more complete solution that takes care of all cases and finds all possible matches. Finally I provided the simple alternative of all the C family of string functions beginning with str* hence my answer above.
0

One for-loop inside another for-loop (called nested loop). Go through all the letters in your array, and for each letter go through all the letters in your input string and find out if that part of the array matches with the input string. If it does, print it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.