Originally Posted by
fireman
WoW!
someone here seems to be pretty impressed with themselves.
You should be careful about the way you talk for other people inside the forum, these people like Salem care for all and try to help, i will give you some code to solve your prob but this is for the last time, learn to work alone and in case of problem ask by showing some code and get your help eventually.
Code:
//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Find a substring inside a string.
int ManualStrStr(const char *string, const char *substring)
{
if(!string || !substring)
{
printf("NULL parameter error, check strings.\n");
return -1;
}
else
{
/* Declare variables. */
int i,j = 0;
int k = 0;
/* Get the string lengths. */
int strLen = strlen(string);
int substrLen = strlen(substring);
if(substrLen > strLen)
{
printf("Error, Substring has bigger length than the main String.\n");
return -2;
}
else
{
for(i = 0; i < strLen; i++)
{
if(substring[j] == string[i])
{
for(k = i, j = 0; j < substrLen && substring[j] == string[k]; j++,k++);
if(j == substrLen)
{
printf("Substring '%s' was found in String '%s'.\n", substring, string);
return 0;
}
else
j = 0;
}
}
/* Exits this for loop means that the string was not found. */
if(i == strLen)
{
printf("Substring '%s' was not found in String '%s'.\n", substring, string);
return -3;
}
else
{
printf("Some kind of error stopped the loop earlier.\n");
return -4;
}
}
}
}
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
char string[128] = "";
char substring[64] = "";
printf("Give me the initial string:");
fgets(string,sizeof(string),stdin);
printf("Give me the potential substring:");
fgets(substring,sizeof(substring),stdin);
string[strlen(string)-1] = 0;
substring[strlen(substring)-1] = 0;
ManualStrStr(string, substring);
printf("Hit enter to continue....\n");
getchar();
return 0;
}
//---------------------------------------------------------------------------