Okay now I have my text from a file. Is there an easy way to search the string other than making some huge function?
Printable View
Okay now I have my text from a file. Is there an easy way to search the string other than making some huge function?
in C
strchr(bigstring, character) looks for a char in a string
str(bigstring, littlestring) looks for a small string in a big one
C++
CString has instr to find substrings.
what header is needed?
error C2065: 'str' : undeclared identifier
CString is MFC, Jim :eek: For normal C++ it's best to use string.
However, if it's a large file (more than about 250K), it can be a lot faster to use a rope.
oops -
strstr(bigstring, littlestring);
my typo.
If you go into VC++ help and look up strcmp( ) or any other string thing - you have a link to all of the functions as well as CString.
Parskie is right - but th eonly serious code I've written in C++ was MFC - a while back. I'm back to C at work now.
parksie: what is a rope? I never heard of it.
I haven't used the STL::rope yet but it's suppose be a string that is good for frequent concentrations of long strings, in contrast to char arrays which would take linear time to concentrate.
http://www.sgi.com/tech/stl/Rope.html
okay this strstr() function is really throwing me off. How do I use it? I just want to know where the little string is in the big string (like vb's InStr). :confused: :confused: :confused:
Code:#include <stdio.h>
void main(void){
char *buf, *s;
int where;
char bigstring[100];
char littlestring[10];
memset(littlestring,'\0',sizeof(littlestring) );/* zero out memory */
memset(bigstring,'\0',sizeof(bigstring) );
strcpy(bigstring,"this is a test."); /* put stuff in test strings */
strcpy(littlestring,"is");
buf = (char*) strstr(bigstring,littlestring); /* do instr( ) */
if (buf != NULL) {/* we found one if buf != NULL */
s = bigstring;
where = buf - s;
printf("position = %d\n",where);
}
else {
printf("Not found. \n");
}
}