Subscribe Us

Responsive Advertisement

Advertisement

INDEX of a string in c without built in function

 

INDEX of a string in c without built in function



//we consider starting index from 1

#include<stdio.h>

int index(char c[],char d[])

{

    int i,j,co=0;

    for(i=0;i<strlen(c);i++)

    {

        co=0;

        if(c[i]==d[0])

        {

            for(j=0;j<strlen(d);j++)

            {


                if(d[j]==c[i+j])

                {

                    co++;

                    continue;

                }

                else break;

            }

        }

        if(co==strlen(d))

        {

            return i+1;

        }

    }

    return 0;

}




int main()

{

    char a[100],b[100];

    gets(a);

    gets(b);

    int r;

    r=index(a,b);

    printf("%d\n",r);



    return 0;

}





Post a Comment

0 Comments