Qbasicnews.com

Full Version: mid$ in c
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
whats the equivelent on mid$ in c and/or c++? Its really pi$$ing me off trying to find it! :normal:
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.
You do know, that in c you can simply to

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
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)
thanx hard rock. I never would of thought of that.
Bare with me, my knowledge of C is slim to null.

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 Wink but the concept is still there.
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"
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....
if your using C++, you can just use the string class, i.e.:

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
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?
SizeOf() should work, too, because every string in C/C++ is an array of bytes....
Pages: 1 2