-5

I have a 2D array of strings loaded with getline from stdin example :

Hi my name is John.
I like chocolate.

Then i want to search if entered string / substring matches with one of the string arrays example :

Ohn. - matches at line 1
chocolate. - matches at line 2

I'm using standart function strstr :

if ( ( strstr(array[i],string) ) != NULL ) {
      printf("Match");
}

The problem is that when i want to find a string which is not at the end of the string like i wrote , it does not match because probably when i want to find "like" in the string it probably compares like\0 with "like" so it will never match.

When i load the line with getline to buffer i used function: strlen(buffer)-1 then i allocated memory for strlen(buffer) - 1 * sizeof(char) and then copied it to the array with memcpy function. Everything worked perfectly but when the string has lenght of 7-8 it puts 2 undefined characters at the end of the string example :

Enter string :testtttt
memcpy to allocated array of strlen(string) - 1
printed string from array looks like : testttttt1� or testtttttqx etc..

Strings with length less then 7 or more than 8 characters work perfectly. If you know how to fix this problem or know a better way to make from string\0 just string without \0 let me know i will be thankful.

Part of the code which does not work. Only matches wtith ending strings like i mentioned.Pole is 2D array of strings, line is buffer where the string is stored.

size_t len = 0;
char *line = NULL;
int number;
while ( (number = getline(&line, &len, stdin ) ) != -1 ) {
    for (i = 0; i < index; i++) {
            if(strstr(pole[i], line) != NULL) {
               printf("Match");
            }
    }
}


    6 
John.

'Hi my name is John.
' contain 'John.
'
'Testing stuff
' does not contain 'John.
'
'I do not know what to write
' does not contain 'John.
'
8 
Testing

'Hi my name is John.
' does not contain 'Testing
'
'Testing stuff
' does not contain 'Testing
'
'I do not know what to write
' does not contain 'Testing
'
5 
know

'Hi my name is John.
' does not contain 'know
'
'Testing stuff
' does not contain 'know
'
'I do not know what to write
' does not contain 'know
'
Clifford
  • 88,407
  • 13
  • 85
  • 165
paxie
  • 137
  • 1
  • 2
  • 9
  • 7
    A string is an array of characters ending with a \0. If it doesn't end with a \0, it's not a string. Also, strstr does **not** count the \0. – user253751 Dec 08 '15 at 08:22
  • So can you then tell me why it does not match all string or substrings ? – paxie Dec 08 '15 at 08:23
  • can you edit the question with the code snippet that you've written? – sameera sy Dec 08 '15 at 08:24
  • @paxie Nope, but I know this isn't your problem. – user253751 Dec 08 '15 at 08:25
  • Also, you *have* got a "string without \0". As you've found out, the \0 is actually there for a reason, which is so that functions like printf and strstr can tell where the end of the string is! – user253751 Dec 08 '15 at 08:29
  • `sizeof(char)` allways equal 1 by definition - there is no need to multiply `strlen(buffer) - 1` by `sizeof(char)`. Rather than *describing* your code, it would be much simpler to just post it. Then we might see what your real problem is. If you have a problem, it is never a good idea to hypothesise what that problem might be (if you knew enough to make a valid hypothesis, you probably would not have a problem) - just ask the question. – Clifford Dec 08 '15 at 08:29
  • *"Strings with length less then 7 or more than 8 characters work perfectly."* - by luck, not judgement. Probably due to 8 byte alignment of the allocation. – Clifford Dec 08 '15 at 08:32
  • Is this even *real* code!? `if (strstr(array[i],string)) != NULL` is invalid - the `!= NULL` is outside the parenthesis. We cannot hope to solve your problem from code that is not the code with the problem or descriptions of code rather than *actual* code! – Clifford Dec 08 '15 at 08:42
  • *"Pole is 2D array of strings" - did you really mean that, or is it just an *array of strings* (which would be a 2D array of `char`s). You are certainly using as an *array of strings* in this code. – Clifford Dec 08 '15 at 09:03
  • I have rolled back your edit because you removed all context than made the accepted answer make any sense. Improving a question is fair; changing a question when it already has an acceptable answer is not. If you no longer think the question is valid, you should delete it, and perhaps ask a new question. The part about your test with "testtttt" is irrelevant, but that was the part you retained in the edit. – Clifford Dec 08 '15 at 11:18

2 Answers2

3

Your problem is evident in your debug output. getline does not strip the newline from the input, so for example you are searching for:

"know\n" 

in

"I do not know what to write\n"

So your problem is not about stripping the \0 string terminator, but rather stripping the \n line-end.

That can be achieved in a number of ways, for example:

char* newline = strrchr( line, '\n' ) ;
if( newlineaddr != NULL )
{
    *newlineaddr  = '\0' ;
}

or

size_t newlineindex = strcspn(line, "\n") ;
line[newlineindex] = '\0' ;

The first copes with multi-line input (not needed in this case) - only removing the last newline, while the second is more succinct.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Posted code and the result after printing two compared strings. – paxie Dec 08 '15 at 08:55
  • Yes thank you that was the problem. – paxie Dec 08 '15 at 09:16
  • 2
    @paxie : The issue would have been immediately obvious when you observed the strings in a debugger. – Clifford Dec 08 '15 at 09:16
  • Your proposed solution will crash if there is no `'\n'` in `line`, which will happen if the last line of the file does not end with a line feed. A more general solution is `line[strcspn(line, "\n")] = '\0';` – chqrlie Dec 08 '15 at 09:34
  • @chqrlie : Good suggestion, though in this case the use of `getline()` ensures a newline. The use of the reverse search was to allow matching multiple lines by only removing the last newline, but again that will never be the case for `getline()`. As I said - a number of ways - the suggestion was not intended to be a recommendation. The OP should consider his choices, and in this case your suggestion would probably be most appropriate. Stealing your idea right now, and fixing the potential null dereference. – Clifford Dec 08 '15 at 10:59
0

Searching through a function in c is very easy.You can use strcmp for comparison and strcmp comes with different flavours of it like stricmp,strncmp and so on...Here is the link

  • "Searching through a *function*"? Did you mean "searching *using* a function" or "searching through a *string*"? But that is not an answer to the question in any case. Searching for a substring is directly supported by the standard library using `strstr()` Doing the same thing in a different way will not solve the problem here which is that the string really does not exist in the line - because it has a stray `\n` appended. – Clifford Dec 08 '15 at 11:05
  • 1
    ... to be fair, @paxie changed the question *after* accepting an answer, so I can see how you might have thought this was helpful. I have rolled-back the question to maintain context. – Clifford Dec 08 '15 at 11:13