fork download
  1. #include <iostream>
  2. using namespace std ;
  3.  
  4. char *strstr(char *str1, const char *str2)
  5. {
  6. int count2= 0, count2_max= 0 ;
  7.  
  8. while (*(str2) != '\0') //recording max value of 2nd string
  9. {
  10. str2++ ;
  11. count2_max++ ;
  12. }
  13. str2 -= count2_max ;
  14. while (*(str1) != '\0') //checking string 1 character by character for if it has string 2 in it.
  15. {
  16. if (*(str2) == *(str1))
  17. {
  18. str2++ ;
  19. count2++ ;
  20. }
  21. else
  22. {
  23. if (*(str1) != *(str1-1))
  24. {
  25. count2= 0 ;
  26. }
  27. }
  28.  
  29. if (count2 == count2_max) //checking if 2nd string is complete in the first string
  30. {
  31. str1=((str1-count2_max) + 1) ;
  32. return str1 ;
  33. }
  34. str1++ ;
  35. }
  36. if (*(str1) == '\0')
  37. {
  38. return NULL ;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. char *str1= new char[100] ; //The first string
  45. char *str2= new char[100] ; //The second string
  46.  
  47. cout << "Please Enter the string: " ; cin.getline(str1, 100) ;
  48. cout << "Now please enter the second string: " ; cin.getline(str2, 100) ;
  49.  
  50. cout << endl << "The pointer to the first occured second string (str2) in the first string (str1) holds the following address: " << (int*)strstr(str1, str2) << endl << endl ;
  51.  
  52. delete []str1 ;
  53. delete []str2 ;
  54. }
  55.  
Success #stdin #stdout 0s 3476KB
stdin
banana
na
stdout
Please Enter the string: Now please enter the second string: 
The pointer to the first occured second string (str2) in the first string (str1) holds the following address: 0x8a1000a