char pointer with for loops
Hi, I need help with this please.
I am trying to get the function to determine if the second string entered by the user is contained in the first string by defining the substr() function. I was able to achieve it with a while loop and a nested if statement. I was wondering if someone could help me figure out how to do it with a nested for loop. The prototype is given below and i am using eclipse. Thanks!
Code:
#include <stdio.h>
#include <string.h>
// returns the starting index of the first such occurrence
// where sub_str was found within str - if one exists
// otherwise returns -1
int substr(const char str[], const char sub_str[]);
int main()
{
char str[255];
printf("Enter a string: "); fflush(stdout);
scanf("%s", str);
char sub_str[255];
do
{
printf("Enter a string to search for (\"quit\" to quit): "); fflush(stdout);
scanf("%s", sub_str);
if ( strcmp(sub_str, "quit") )
{
int ind = substr(str, sub_str);
if ( ind == -1 )
{
printf("Not found: %s\n", sub_str);
}
else
{
printf("\"%s\" found at starting index %d\n", sub_str, ind);
}
}
} while ( strcmp(sub_str, "quit") );
printf("Done!\n");
return 0;
}