Greetings,
But i bet you can make your own functions.
Anyway, here's an example on how to find a string inside another:
Code:
#include <iostream> // This is the standard header. 'iostream.h' is deprecated
// and should no longer be used
// using namespace std; // uncomment this line if you don't want to type std::
// before every 'cout', 'cin', 'endl', etc.
int string_length(const char* string);
int string_find(const char* source, const char* what);
int main()
{
char firstString[100], secondString[100];
int found = 0;
std::cout << "Enter first string: ";
std::cin.getline(firstString, 100); // I'm using getline because this way I can input more than
// one word.
std::cout << "Enter second string: ";
std::cin.getline(secondString, 100);
found = string_find(firstString, secondString);
if (found != -1)
std::cout << "Found at position " << found << std::endl;
else
std::cout << "Not found." << std::endl;
return 0;
}
int string_length(const char* string)
{
int length = 0;
while (string[length] != 0)
++length;
return length;
}
int string_find(const char* source, const char* what)
{
int len_source = string_length(source);
int len_what = string_length(what);
for (int i=0;i<len_source;++i) {
if (source[i] == what[0]) { // We have a match for the first char. Let's check the rest...
for (int j=0;j<len_what,source[i+j] == what[j];++j);
if (j >= len_what) // '>=' because if 'what is at the end of 'source' j=len_what+1
return i; // return the position where 'what' was found
}
}
return -1; // -1 -> not found
}
Sample output:
Code:
Enter first string: The rain in Spain stays mainly in the plane.
Enter second string: Spain
Found at position 12
Enter first string: The rain in Spain stays mainly in the plane.
Enter second string: plane
Found at position 38
Enter first string: The rain in Spain stays mainly in the plane.
Enter second string: The
Found at position 0
Enter first string: The rain in Spain stays mainly in the plane.
Enter second string: whatever
Not found.