I've got the following code (instructions are at the top) and I can't seem to work out how I would get the user input, then make than into a const char * data type so I can use strstr on it. Works fine when I provide the strings as a constant of course, but I need to convert a variable to a constant pointer...
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
// Input a line of text and a search string.
// Using strstr, locate the first occurrence of searchstring in text.
// If the searchstring is found:
// Assign the location to searchPtr (type char *).
// Print the remainder of the line (beginning with searchPtr).
// Use strstr again (using searchPtr + 1) to find the next
// occurence of searchstring.
// Print the remainder of the line again.
char *Text;
char *SearchString;
cout << "\nInput a line of text to search: " << endl;
cin >> Text;
cout << "\n\nInput a search string to find: " << endl;
cin >> SearchString;
char *searchPtr = strstr(Text, SearchString);
cout << searchPtr << endl;
cout << strstr(searchPtr+1, SearchString);
cout << endl;
return 0;
}
strstrcan be used with (non-const) char* arguments. The fact that it takes constchar* simply means that it is guranteed that it won't modify your strings.
The problem here is that Test and SearchString are pointers. No memory is reserved to hold the user input, thus the lines cin >> Text; and cin >> SearchString; overwrite data that may be useful to your program and the program crashes as a result (if it didn't crach you simply were lucky. It could crash anytime!). A solution to this could be declaring Text and SearchString as arrays of characters, like this:
1 2
char Text[1024];
char SearchString[32];
Another thing is that using operator >> for input doesn't allow you to have spaces in the data inputted. Use getline instead, like this:
Thanks - that worked! I'm trying to modify it to accept multiple lines of text though and having a helluva time... Trying to use get to get the string and loop through a search like so:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char Text[1024];
char SearchString[32];
cout << "\nInput a line of text to search: " << endl; // Input a line of text and a search string.
cin.get(Text, 1024, EOF);
cout << "\n\nInput a search string to find: " << endl;
cin.get(SearchString, 32);
char *searchPtr;
if (searchPtr = strstr(Text, SearchString)) // If you find the searchstring
{
cout << searchPtr << endl; // Print the remainder of the line (beginning with searchPtr)
while (searchPtr = strstr(searchPtr+1, SearchString)) // Use strstr again (using searchPtr + 1) to find
{ // subsequent occurences of searchstring.
cout << searchPtr << endl;
}
}
cout << endl;
cout << endl;
cout << endl;
return 0;
}
It compiles and runs but after I input the search strings and do a CTRL+z, nothing happens. I do another CTRL+z and I get an eruption of garbage:
Found a way to get multiple lines of text into a string but I can't use strstr on a string and can't convert it to a char *... Pulling my hair out here!