1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include<iostream>
#include<cctype>
#include<cstring>
#include<fstream>
using namespace std;
void read();
void ignoreRead(char []);
void writeOver(char [], char [], char []);
void otherway();
bool search(char [], char []);
int main()
{
//char word1[20], sub1[20];
read();
return 0;
}
void read()
{
char word1[20];
char sub1[20];
char sentence[1000];
char no[] = "$";
ifstream infile;
infile.open("paragraphwordssub.txt");
ignoreRead(sentence);
infile >> word1;
cout << word1 << endl;
infile >> sub1;
cout << sub1 << endl;
while (word1[0] != no[0])
{
writeOver(word1, sub1, sentence);
cout << sentence << endl;
infile >> word1;
cout << word1 << endl;
infile >> sub1;
cout << sub1 << endl;
}
infile.close();
}
void ignoreRead(char sentence2[1000])
{
ifstream infile;
infile.open("paragraphwordssub.txt");
char word = '$';
char ignore[20];
char sentence1[1000];
//char sentence2[1000];
infile >> ignore;
while (ignore[0] != word)
{
infile >> ignore;
}
infile.getline(ignore, 100);
infile.getline(sentence1, 1000);
infile.getline(sentence2, 1000);
//cout << sentence1 << endl;;
//cout << sentence2 << endl;
}
void writeOver(char word1[20], char sub1[20], char sentence[1000])
{
char *sptr;
char temp[1000]="";
int len1, len2, len3;
bool found = true;
sptr = strstr(sentence, word1);
//cout << sptr << endl;
found = search(word1, sentence); //added this
while (found)
{
len1 = strlen(sentence);
len2 = strlen(sptr);
len3 = len1 - len2;
strncpy(temp, sentence, len3);
strcat(temp, sub1);
strcat(temp, sptr + strlen(word1));
for (int x = 0; x < 1000; x++)
{
sentence[x] = temp[x];
}
found = search(word1, sentence);
sptr = strstr(sentence, word1);
}
//cout << sentence << endl;
}
bool search(char word1[20], char sentence[1000])
{
char *sptr = NULL;
sptr = strstr(sentence, word1);
//cout << sptr << endl;
if (sptr == NULL) ///this is where the compiler crashes
{
return false;
}
else
{
return true;
}
}
|