Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C PROGRAMMING LANGUAGE!! Write a function called findString() to determine if

ID: 645203 • Letter: I

Question

IN C PROGRAMMING LANGUAGE!! Write a function called findString() to determine if one character string exists inside another string. The first argument to the function should be the character string that is to be searched adn the second argument is the string you are interested in finding. If the function finds the specified string, have it return the location in the source string where the string was found. If the function does not find the string, have it return -1. in example us "Chatterbox" and "hat"

One additional requirement beyond what is stated in the text is that square brackets "[ ]" are not allowed in the function. In other words, use pointers to access the character strings rather than array notation.

NOTE: Square brackets are fine in "main".

Explanation / Answer

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

int findString(char *a,char *str)
{
int loc=0,i=0;
char *p=a;
char *s=str;
int comp=0;
while(*s)
{
if(*s==*p)
{
if(comp==0)
loc=i;
s++;p++;
i++;
comp++;
}
else
{
i++;
p=a;
s++;
comp=0;
}

if(*p==NULL)
{
return loc;
}
}
return -1;
}

int main()
{
printf("%d",findString("bat","Chatterbox"));
}