Computer Scientists are Pretty Pessimistic

Thursday 1 October 2015

WAP to search a given string into another string and displays the position if found otherwise displays 0.

CODE ::

#include <stdio.h>      //This code is case sensitive
#include <string.h>
int main ()
{
    char a[100],b[100];
    int i,w=1,k,l=0,j;
    printf("Enter string: ");
    gets(a);
    printf("Enter substring: ");
    gets(b);
    for(i=0;i<a[i];i++)
    {
        if(a[i]==' '||a[i]=='.'||a[i]==','||a[i]=='?'||a[i]=='!'||a[i]==';'||a[i]==':')
        {
            if(a[i+1]>='a'&&a[i+1]<='z'||a[i+1]>='A'&&a[i+1]<='Z')
                w++;
        }

        if(a[i]==b[0])
        {
            for(k=i,j=0;j<b[j];k++,j++)
            {
                if(a[k]==b[j])
                    l++;
            }
        }
        if(l==strlen(b))
            break;
    }
    if(l==strlen(b))
        printf("\nSub-String found in position %d\n",w);
    else
        printf("0");
    return 0;
}

No comments:

Post a Comment