I want to search an element of the string str in the string s but when I'm trying to search anything(e.g. str[1]) in the string s, it returns 0

Utkarsh Pandey Source
#include <stdio.h>
#include <string.h>

int lsearch(char a[],int l,int h,char p)
{
   int i=l;
   for(i=l;i<=h;i++)
      if(a[i]==p)
        return i;
      else
        return 0;
}

int main()
{
   int i,z;
   char s[100];
   scanf("%s",s);
   char str[100];
   scanf("%s",str);
   z=lsearch(s,0,strlen(s)-1,str[1]);
   printf("%d",z);
}

I want to search an element of the string str in the string s but when I'm trying to search anything(e.g. str[1]) in the string s, it returns 0

cstringalgorithmloopsfind

Answers

answered 3 months ago usr #1

You need to continue the loop when the element is not found. The function currently returns on first mismatch.

Something like:

    ...

    for(i=l;i<=h;i++) {
        if(a[i]==p) return i;
    }
    return -1;
}

Note that I changed to return -1 on failure because 0 could also be the index where the char is found.

answered 3 months ago sg7 #2

Your loop exits after ONLY one check: It will return 0 for first return i or 0 for second return 0.

for(i=l;i<=h;i++)
{
    if(a[i]==p)
        return i;  // 0
    else
       return 0;   // 0
}

The loop should go on:

#include <stdio.h>
#include <string.h>

    int lsearch(char a[], int l, int h, char p)
    {
        int i;    
        for(i=l; i < h; i++){

            if( a[i] == p){
                return i;
            }
        }
       return -1;
    }

    int main()
    {
        int i,z;
        char s[100];

        scanf("%s",s);

        char str[100];
        scanf("%s",str);

        z = lsearch(s, 0 ,strlen(s), str[1]);

        printf("%d",z);
    }

test:

12345                                                                                                                                       
13                                                                                                                                          
2

answered 3 months ago Vlad from Moscow #3

If to rewrite your for loop with using braces then it looks like

for(i=l;i<=h;i++)
{
    if(a[i]==p)
        return i;
    else
        return 0;
}

So it is seen that if the first (at position l) character of the source string is not equal to the target character the function returns 0.

Also it is a bad approach to return 0 becaues the parameter L can be initially set to 0 by the user. In this case it is unclear whether the character is found or not.

Moreover the position should have the type size_t and the source character array should be specified with the qualifier const because it is not changed in the function.

The function can look as it is shown in the demonstrative program.

#include <stdio.h>

size_t lsearch( const char s[], size_t n, char c )
{
    size_t i = 0;

    while ( i < n && s[i] != c ) ++i;

    return i;
}

int main(void) 
{
    char s[] = "Hello Utkarsh Pandey";
    size_t start_pos = 6;
    size_t n = 7;

    size_t pos = lsearch( s + start_pos, n, 'a' );

    if ( pos != n )
    {
        printf( "The character 'a' is found at position %zu\n", pos + start_pos );
    }
    else
    {
        puts( "The character 'a' is not found" );
    }

    return 0;
}

The program output is

The character 'a' is found at position 9

comments powered by Disqus