djelad1

Untitled

Dec 5th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. const int LETTERS = 50;
  5. const int LINE = 1;
  6.  
  7. void main()
  8. {
  9. int i = 0, j, counter = 0, sizeOfStringOne = 0, sizeOfStringTwo = 0, result;
  10. char str1[LINE][LETTERS], str2[LETTERS];
  11.  
  12. cout << "please enter a string (max " << LETTERS - 1 << " letters): ";
  13. for (i = 0; i < LINE; i++)
  14. cin.getline(str1[i], LETTERS);
  15.  
  16. cout << "please enter another string (max " << LETTERS - 1 << " letters): ";
  17. cin.getline(str2, LETTERS);
  18.  
  19. sizeOfStringTwo = strlen(str2);
  20. sizeOfStringOne = strlen(str1[1]);
  21. for (i = 0, j = 0; sizeOfStringOne>0 && counter != sizeOfStringTwo; sizeOfStringOne--, i++)
  22. {
  23. for (; str1[1][i] == str2[j]; j++, i++)
  24. {
  25. counter++;
  26. if (counter == 1)
  27. {
  28. result = i;
  29. }
  30. }
  31. if (counter != sizeOfStringTwo)
  32. counter = 0;
  33. }
  34. if (counter == sizeOfStringTwo)
  35. cout << "\nthe second string begin in index " << result << " in the first string.\n\n";
  36. else
  37. cout << "\n" << -1 << "\n\n";
  38. system("pause");
  39. system("cls");
  40. }
Advertisement
Add Comment
Please, Sign In to add comment