searching in array..

hi friends..
i have an array whith 22306 variable in it..chars..dinamic array

not i'd search a name by the array..

char *array;

array = new char [length];
...
delete [] array;

now there are a structure of array same this:

array[1] = c
array[2] = o
array[3] = d
array[4] = e
array[5] = g
array[6] = u
array[7] = r
array[8] = u

i would show only code...
but i don't use for stratmente , becouse the word "code"
isn't in the same position..
i have the text in a file, and whith fstream, i convert it in an array, so
if i change text, the program must verify in whot position code is,
and how long is it "code" = 4

how can i do professionally??

thnaks!!...
[799 byte] By [Rooting] at [2007-11-20 11:38:46]
# 1 Re: searching in array..
Use a std::string with its find() member function. Also, isnt "code" always 4 letters long, regardless of where the word is in a string?
laserlight at 2007-11-9 1:25:58 >
# 2 Re: searching in array..
yeah is the same..
but to convert array whith string, can i use the template
by philip nicoletti??

template <typename T1, typename T2>
T2 Convert(const T1 & t1)
{
stringstream ss;
ss << t1;
T2 t2 = T2();
ss >> t2;
return t2;
}

there convert in a string an array, so is right..isn't it??
and whot i do in [] of array??..

Convert<char, string>(array[??])
Rooting at 2007-11-9 1:26:58 >
# 3 Re: searching in array..
std::string has constructors that can used. If your array of char is actually a null terminated string, you can just write:
std::string str(array);

If it is not null terminated, then you can write:
std::string str(array, array + length);
laserlight at 2007-11-9 1:27:57 >
# 4 Re: searching in array..
to take the data i use the function

is.open ("file.dat", ios::binary );

is.read (buffer,length);

and read function has a costructor whith a char not string..
so i convert it...
Rooting at 2007-11-9 1:28:59 >
# 5 Re: searching in array..
In that case you are not necessarily dealing with a null terminated string so use the second option for the constructor.
laserlight at 2007-11-9 1:29:58 >
# 6 Re: searching in array..
The C runtime function strstr would do it directly on your char array.
GCDEF at 2007-11-9 1:31:08 >
# 7 Re: searching in array..
ohh thanks!!..
I resolve it whith str.find...
to show whot position are the word i use
found=str.find(find);
and call it whith int(found)

but if not exist whot i do??..
Rooting at 2007-11-9 1:32:09 >
# 8 Re: searching in array..
for string::find() the function returns string::npos if not found ...

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
char pstr[] = "this is a test : dev-archive";

const int length = 35;

string str(pstr,length);

size_t pos = -1;

char * ps = strstr(pstr,"code");

if (ps != 0) // found
{
pos = ps - pstr;
cout << "position using char array = " << pos << "\n";
}

pos = str.find("code");

if (pos != string::npos) // found
{
cout << "position using std::string = " << pos << "\n";
}

return 0;
}
Philip Nicoletti at 2007-11-9 1:33:01 >
# 9 Re: searching in array..
ohh thanks!!!!!!...
very gentilment!!!!!

Se you asap philip!!..
Rooting at 2007-11-9 1:34:02 >