05-31-2003, 01:06 PM
Pages: 1 2
05-31-2003, 01:55 PM
Quote:Code:char *strstr(
const char *string,
const char *strCharSet
);
Find a substring.
Parameters
string - Null-terminated string to search.
strCharSet - Null-terminated string to search for.
Return Value
Returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.
Remarks
The strstr function returns a pointer to the first occurrence of strCharSet in string. The search does not include terminating null characters. wcsstr and _mbsstr are wide-character and multibyte-character versions of strstr. The arguments and return value of wcsstr are wide-character strings; those of _mbsstr are multibyte-character strings. These three functions behave identically otherwise.
Keep in mind that strstr("qbasic programming is cool", "is") will return a pointer to the I in "is", so if you print it you'll see "is cool":
cout<<strstr("qbasic programming is cool", "is"); // prints "is cool"
If you just want the "is", you'll have to make sure to copy out only 2 characters into a new string and then add on a NULL to terminate it.
05-31-2003, 07:03 PM
You do know, that in c you can simply to
Hope that helps, you dont need mid in c becuase you can access anyof the string at will.(and with this, it should be VERY easy to write your own, using sizeof() to calculate size of the sting)
Code:
printf("%c",string[numberhere]);
//So basically if you have
string = "string name"
//and did
if(string[0]='s')
//It would be true
//or
printf("%c",string[0]);
//Would print s
05-31-2003, 09:28 PM
thanx hard rock. I never would of thought of that.
05-31-2003, 09:57 PM
Bare with me, my knowledge of C is slim to null.
If you combine the 2 of those, I think you can do this:
Although I just realized that that's useless, since all it does is basically prints out searchstring for you but the concept is still there.
If you combine the 2 of those, I think you can do this:
Quote:string = "string name";
searchstring = "g na";
length = sizeof(searchstring);
position = strstr(string, stringsearch);
endposition = length + position;
for (i = position; i < endposition; i = i + 1) {
printf("%c",string[i]);
}
Although I just realized that that's useless, since all it does is basically prints out searchstring for you but the concept is still there.
05-31-2003, 10:31 PM
lith helped me make a mid$ sub for plain string byte arrays for ante87 once (i've lost my icq logs since), but i believe in most standard string classes there's an equivalent called "substr"
06-01-2003, 12:44 AM
Ive got what i wanted now thanx guys. that sub string you metion toonski gave me $hit. It was what popped up when I googled for a "c substring function".
It's ment to be:
variblename.substr(start, length);
but it brought up errors when I compiled! and yes, I did include <string,h> (which is useless cause substr isnt defined in there :lol: ). It must be in some other header file....
It's ment to be:
variblename.substr(start, length);
but it brought up errors when I compiled! and yes, I did include <string,h> (which is useless cause substr isnt defined in there :lol: ). It must be in some other header file....
06-11-2003, 05:30 AM
if your using C++, you can just use the string class, i.e.:
hope that helps
Code:
// example using the C++ string class
#include <iostream>
#include <string>
using namespace std;
int main (void)
{
string name;
string first = "Chris", last;
last = "Adams";
name = first + " " + last; // Just like QB
cout << "name = " << name << endl;
cout << "name[6] = " << name[6] << endl;
string middle = name.substr(3, 5); // Mid$(name, 4, 5)
cout << "Marijuana " << middle << "dictive." << endl;
return 0;
}
hope that helps
06-14-2003, 06:27 AM
Quote:Hope that helps, you dont need mid in c becuase you can access anyof the string at will.(and with this, it should be VERY easy to write your own, using sizeof() to calculate size of the sting)
I didn't think you could use sizeof... wouldn't you use strlen?
06-14-2003, 06:34 PM
SizeOf() should work, too, because every string in C/C++ is an array of bytes....
Pages: 1 2