-4

I've written a for loop that searches a string in another. I want it to rename it's position when it finds it. ( I am open for new suggestions for the search :) )

int search (char str1[56], char str2[5]){
    int c1, c2, c3, c4;
    c1 = 0;

    for (c1=0; c1<56; c1++)
    {
        c2 = 0, c3 = 0;
        for (c2 = 0; c2 < 5; c2++){
            if (str1[c1] == str2[c2]){
                c3++;
            }
        }

        if (c3 == 5){
            c4 = c1; 
            break;
        }
    }

    return c4;
}
cskr
  • 77
  • 2
  • 9

4 Answers4

3

It sounds like you want strstr(), it searches for a substring in a string and returns a pointer to it if found. Strings aren't the best thing to start with when learning C, you really want to use proper libraries for that at first.

Jite
  • 4,250
  • 1
  • 17
  • 18
  • I would look for the current implementation of strstr() as well. One example is here: http://clc-wiki.net/wiki/C_standard_library:string.h:strstr – Manoj Pandey Dec 05 '13 at 19:37
  • I tried using 'strstr' but didn't succeeded it. I couldn't return the point of match. How can I return the match point with 'strstr' ? – cskr Dec 05 '13 at 19:53
  • `strstr()` returns a pointer to the match point. If you want an integer index, you can then subtract the address of the string from the address returned by `strstr()` and you will get an integer difference. – steveha Dec 05 '13 at 20:04
  • But I'm trying to find the c1 value for the match? Will that difference **absolutely** give me that? – cskr Dec 05 '13 at 20:08
1

in the innermost for loop you iterate over all characters of str2, but compare each of them to the same position in str1

for (c2=0; c2<5; c2++) {
    if (str1[c1] == str2[c2]) {
        c3++;
    }
}

what you want is to iterate over the characters of str1 too, i.e.

for (c2=0; c2<5; c2++) {
    if (str1[c1+c2] == str2[c2]) {
        c3++;
    }
}

this will run out of bounds on str1, you can fix this by searching only up to position length(str1) - length(str2) + 1

for (c1=0; c1<56-4; c1++) {

you should however consider not using hardcoded string lengths

pepo
  • 669
  • 4
  • 7
0

As @Jite suggested, you can go for strstr(), that's the best option. If you want add another flavor to your search, regarding comparing two strings while iGnOrInG the case, you can give a try to strcasestr().

While using either of them, don't forget to include string.h

Check out the man page of strstr() for more information. Also, for sample usage check this.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0
#include <stdio.h>
#include <string.h>

int search(const char str1[56], const char str2[5]){
    int c1, c2;
    int len1 = strlen(str1);
    int len2 = strlen(str2);

    for (c1=0; c1<len1-len2+1; ++c1){
        for (c2 = 0; c2 < len2; ++c2)
            if (str1[c1+c2] != str2[c2])
                break;
        if (c2 == len2)
            return c1;
    }

    return -1;//not found
}
int main(void){//DEMO
    const char *test_data[] = { "test", "the", "pro", "gram", "hte" };
    int i, size = sizeof(test_data)/sizeof(*test_data);
    for(i=0;i<size;++i){
        int result;
        result=search("test the program!", test_data[i]);
        printf("%d\n", result);//0, 5, 9, 12, -1
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • thank you very much, this was most helpful. one last thing, I couldn't understand what you did here: `int i, size = sizeof(test_data)/sizeof(*test_data);` would you mind explaining it to me? :) – cskr Dec 05 '13 at 23:07
  • @cskr It is asking (eg case of `test_data[5]` is 5) the size of the array. You will be prompted by dividing size of the contents of one of the array (eg sizeof(*test_data)) the overall size of the array(eg `sizeof(test_data)`) by using the `sizeof` operator. That way, followed by the addition of data This is useful sometimes. – BLUEPIXY Dec 05 '13 at 23:17
  • case of `char*` (eg size = 8): `char *array[5]` : sizeof(array) == total size 40, sizeof(array[0]) == one element size 8, 5 == 40/8. – BLUEPIXY Dec 05 '13 at 23:22