JC wrote:
[color=blue]
> Hello,
> Can I convert the entire content of an array into a string?[/color]
Yes. Each element must be transformed into either a string or
a character, then appended to the string (you didn't specify
the type of the array).
[color=blue]
> for example
> array[i] = contains the value of {h, e, l, l, o}
> I need to complare the entire value with something else not just the last
> value (o)[/color]
If the array is an array of characters, you could always complare [sic]
each element of the array with an element of the string:
string hello_string("hello");
const char * hello_c_string = "hello";
[1] if (array[0] == hello_string[0])
...
if (array[0] == hello_c_string[0])
...
[color=blue]
>
> array[i] == string;[/color]
You can only compare an element of an array to a string if the
each element of the array is a string:
string many_strings[400];
string hello_string("hello");
if (many_strings[0] == hello_string)
....
If you want to compare arrays of characters to C-Style strings
{which are arrays of characters with a NUL ('\0') character
at the end} there are many functions to help you:
strcmp() -- both parameters must be C-style strings.
strncmp() -- useful for arrays of characters or C-Style strings.
std::string::c_str() -- Useful for converting an std::string
into a C-style string.
By far, the easiest method for handling text is to use the
std::string class. This class overloads the equality operator
(==) (as well as other operators) for comparing strings:
string hello_string("Guten Tag");
string world_string("Welt");
if (hello_string == world_string)
See the FAQs below for more helpful information.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book