stacks and array

J

JC

Hello,
Can I convert the entire content of an array into a string?

for example
array = 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)

array == string;

Thanks,
Jc
 
R

Ron Natalie

JC said:
Hello,
Can I convert the entire content of an array into a string?

for example
array = contains the value of {h, e, l, l, o}


What is array? The above is NOT any sort of proper definition.
I need to complare the entire value with something else not just the last
value (o)

Where did this array come from? What have you tried so far. The trivial case
is as follows.

char array[] = { 'h', 'e', 'l', 'l', 'o' }; // 5 element array with hello in it.

string str(array, sizeof(array)); // string initialized with the array.

if(str == "hello") { ...
 
T

Thomas Matthews

JC said:
Hello,
Can I convert the entire content of an array into a string?

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).

for example
array = 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)


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])
...
array == string;


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top