This should be done in C programing (not c++), im using the book, programming in
ID: 3545627 • Letter: T
Question
This should be done in C programing (not c++), im using the book, programming in C by kochan), but its at least for me hard to understand :S
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 and 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
Explanation / Answer
Your function is almost correct with minor correction: Please have a look:
#include <stdio.h>
#define false 0
#define true 1
// find s1 inside source, return index number if found, -1 if not found
int findString(const char source[], const char s[])
{
int i, j, foundit = false; // try each character in source
for (i = 0; source[i] != '' && !foundit; ++i)
{
foundit = true; // now see if corresponding chars from s match
for (j = 0; s[j] != '' && foundit; ++j)
if(source[j + i] != s[j] || source[j + i] == '')
foundit = false;
if(foundit)
return i;
}
return -1;
}
int main()
{
int index = findString("a chatterbox", "bo");
printf("%d", index);
scanf("%d", &index);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.