Скачиваний:
31
Добавлен:
02.05.2014
Размер:
866 б
Скачать
#include "string.h"
#include "iostream.h"
#include "stdio.h"

int search(char string[], char substring[]) {
	int sl, ssl;
	int res = -1;
	int i,j;
	
	sl = strlen(string);
	ssl = strlen(substring);

	if ( sl == 0 ){ 
		cout << "Wrong string "; 
		return -1; 
	}
	if ( ssl == 0 ){ 
		cout << "Wrong substring "; 
		return -1; 
	}

	for (i = 0; i < sl - ssl + 1; i++){
		for (j = 0; j < ssl; j++){
			if ( substring[j] != string[i+j] ){
				break;
			}
			else if ( j == ssl - 1 ){
				res = i;
				break;
			}
		}
	}
	return res;
}

void main(){
	char *s,*p;
	int nMax = 1000;
	s = new char[nMax];
	p = new char[nMax];

	cout << "Simple Search Algoritm.\nString: ";
	cin >> s; 	 
	cout << "Substring: ";
	cin >> p;
	cout << "Result: " << search(s,p);
	cout << "\nPress any key to continue... " << endl;
	getchar();
}