Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- const int LETTERS = 50;
- const int LINE = 1;
- void main()
- {
- int i = 0, j, counter = 0, sizeOfStringOne = 0, sizeOfStringTwo = 0, result;
- char str1[LINE][LETTERS], str2[LETTERS];
- cout << "please enter a string (max " << LETTERS - 1 << " letters): ";
- for (i = 0; i < LINE; i++)
- cin.getline(str1[i], LETTERS);
- cout << "please enter another string (max " << LETTERS - 1 << " letters): ";
- cin.getline(str2, LETTERS);
- sizeOfStringTwo = strlen(str2);
- sizeOfStringOne = strlen(str1[1]);
- for (i = 0, j = 0; sizeOfStringOne>0 && counter != sizeOfStringTwo; sizeOfStringOne--, i++)
- {
- for (; str1[1][i] == str2[j]; j++, i++)
- {
- counter++;
- if (counter == 1)
- {
- result = i;
- }
- }
- if (counter != sizeOfStringTwo)
- counter = 0;
- }
- if (counter == sizeOfStringTwo)
- cout << "\nthe second string begin in index " << result << " in the first string.\n\n";
- else
- cout << "\n" << -1 << "\n\n";
- system("pause");
- system("cls");
- }
Advertisement
Add Comment
Please, Sign In to add comment