Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Da se najde rastojanie megju dva zbora vo tekst.
- #include <iostream>
- #include <cstring>
- using namespace std;
- int main()
- {
- char text[500], firstWord[100], secondWord[100], currentWord[100];
- int n = 0, p = 0, firstWordLength = 0, secondWordLength = 0, firstIndex[50], secondIndex[50], firstIndex_n = 0, secondIndex_n = 0;
- cout << "Vnesi tekst: ";
- cin.getline(text, 500);
- cout << "Vnesi zbor: ";
- cin >> firstWord;
- cout << "Vnesi zbor: ";
- cin >> secondWord;
- cout << endl;
- if (strcmp(firstWord, secondWord) == 0)
- {
- cout << "Zborovite ne mozhe da bidat isti!" << endl;
- return 1;
- }
- firstIndex[0] = secondIndex[0] = -1;
- do {
- firstWordLength++;
- } while (firstWord[firstWordLength] != '\0');
- do {
- secondWordLength++;
- } while (secondWord[secondWordLength] != '\0');
- for (int i = 0;;++i)
- {
- if (text[i] == '\0')
- {
- currentWord[n] = '\0';
- if (strcmp(firstWord, currentWord) == 0)
- {
- firstIndex[firstIndex_n++] = i + 1 - p - firstWordLength;
- }
- else if (strcmp(secondWord, currentWord) == 0)
- {
- secondIndex[secondIndex_n++] = i + 1 - p - secondWordLength;
- }
- break;
- }
- else if (!(text[i] == ',' || text[i] == '.' || text[i] == ';' || text[i] == ':' || text[i] == '!' || text[i] == '?'))
- {
- if (text[i] == ' ')
- {
- currentWord[n] = '\0';
- if (strcmp(firstWord, currentWord) == 0)
- {
- firstIndex[firstIndex_n++] = i + 1 - p - firstWordLength;
- }
- else if (strcmp(secondWord, currentWord) == 0)
- {
- secondIndex[secondIndex_n++] = i + 1 - p - secondWordLength;
- }
- n = 0;
- }
- else
- {
- currentWord[n++] = text[i];
- p = 0;
- }
- }
- else
- {
- p++;
- }
- }
- if (firstIndex[0] == -1 || secondIndex[0] == -1)
- {
- cout << "Nekoj od zborovite (ili dvata) ne se pojavuva vo tekstot!" << endl;
- return 0;
- }
- for (int i = 0; i < firstIndex_n; ++i)
- {
- for (int j = 0; j < secondIndex_n; ++j)
- {
- if (firstIndex[i] > secondIndex[j])
- {
- cout << "Rastojanieto megju prviot i vtoriot zbor e " << firstIndex[i] - (secondIndex[j] + secondWordLength) << endl;
- }
- else
- {
- cout << "Rastojanieto megju prviot i vtoriot zbor e " << secondIndex[j] - (firstIndex[i] + firstWordLength) << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment