5

I have a text file and would like to check if it contains a string. The text file is stored in a char array: char buffer[512][128];.

Q1: Is it easier if I buffer it in one long array instead of a multidimensional array? Q2: How could I check whether the buffer contains the string char *s1 = "Hello world!";? I will need to search for multiple strings.

3
  • Are the multiple strings you're searching for constant, or user-supplied arguments? Are you looking to match any, or all, or some amount in between? strstr will work but we can optimize based on some of those requirements. Commented Dec 29, 2010 at 20:34
  • @Chris Lutz The strings are all different... I have an array with those. I would check if they exist in the old file, if not then i'd add them Commented Dec 29, 2010 at 20:38
  • Okay then. Since you need to check each one individually and add them if they exist, strstr is the way to go. Commented Dec 29, 2010 at 20:44

2 Answers 2

12
  1. It will be much easier to use a single 1D array.

  2. strstr(buffer, s1) != null (once you've changed buffer into a 1D array)

Sign up to request clarification or add additional context in comments.

Comments

9

strstr

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.