strstr error

I'm using strstr to search a c-string but it doesn't return false. The compiler just crashes when it checks for the value. What can I do to fix this?

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
#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;
	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;
	}
}
Last edited on
I'd guess that the problem occurs on line 85. What happens if line 80 return NULL?
I added a check for found on line 81 so it won't enter the loop if it returns NULL, but it still crashes out?

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;
	}
}
please post a small sample of your input file.

Also why are you using the C-strings instead of the C++ string?


Edit: And a little documentation as to what each function is trying to accomplish would be wonderful.
Last edited on
The read function reads in a word then a word that it wants to sub it with.

The ignoreRead function reads in the paragraph that I'm swapping words in it skips the words I swap.

The search function looks to see if the word I want to swap is in the paragraph.

The writeover function swaps the words.

Input file looks like this but is different words but I want it to work for any file that has this format:
{
word word1
wahy wodue
adlfj idejrd

$

word the wahy a word apple wahy tin adlfj this word wahy
}

I want to swap the first word for the second word for every time the word occurs

I am using c-strings because I have already written this in strings and I am trying to do it the old way.

If I run it without the while(found) loop in the writeover function it only replaces the first word even if the word is in there multiple times
Last edited on
Topic archived. No new replies allowed.