Guest User

Untitled

a guest
Nov 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #include <fstream>
  7. using std::ifstream;
  8. using std::ofstream;
  9. using std::ios;
  10.  
  11. struct string
  12. {
  13. size_t size = 0;
  14. char *charArray = new char[ 21 ]();
  15. };
  16.  
  17. // prints all English words which are contiguous substrings of inputtedWord into a file
  18. void embeddedWords( string &dictionaryWord, string &inputtedWord );
  19.  
  20. // returns true if and only if dictionaryWord is a substring of inputtedWord
  21. bool isSubstring( string &dictionaryWord, string &inputtedWord );
  22.  
  23. int main()
  24. {
  25. string inputtedWord;
  26. cout << "Enter a word: ";
  27. cin >> inputtedWord.charArray;
  28. inputtedWord.size = strlen( inputtedWord.charArray );
  29.  
  30. string dictionaryWord;
  31.  
  32. embeddedWords( dictionaryWord, inputtedWord );
  33.  
  34. system( "pause" );
  35. }
  36.  
  37. void embeddedWords( string &dictionaryWord, string &inputtedWord ){
  38. ifstream infiletext("Dictionary.txt", ifstream::in);
  39. ofstream outfiletext("word.txt",ofstream::out);
  40. if(!infiletext){
  41. cout<<"error"<<endl;
  42. }
  43. else{
  44. while(infiletext>>dictionaryWord.charArray){
  45. dictionaryWord.size=strlen(dictionaryWord.charArray);
  46. if(dictionaryWord.size<=inputtedWord.size && isSubstring(dictionaryWord,inputtedWord)){
  47. cout<<dictionaryWord.charArray<<endl;
  48. outfiletext<<dictionaryWord.charArray<<endl;
  49. }
  50. }
  51. delete[] inputtedWord.charArray;
  52. delete[] dictionaryWord.charArray;
  53. }
  54. }
  55.  
  56. bool isSubstring( string &dictionaryWord, string &inputtedWord ){
  57. for (int i = 0; i <= inputtedWord.size - dictionaryWord.size; i++) {
  58. bool flag = true;
  59. for (int j = 0; j < dictionaryWord.size; j++)
  60. if (dictionaryWord.charArray[j] != inputtedWord.charArray[i + j]) {
  61. flag = false;
  62. break;
  63. }
  64. if (flag)
  65. return true;
  66. }
  67. return false;
  68. }
Add Comment
Please, Sign In to add comment