Connecting Tech Pros Worldwide Help | Site Map

stacks and array

JC
Guest
 
Posts: n/a
#1: Jul 19 '05
Hello,
Can I convert the entire content of an array into a string?

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)

array[i] == string;

Thanks,
Jc


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 19 '05

re: stacks and array


"JC" <keepit@secret.com> wrote...[color=blue]
> Can I convert the entire content of an array into a string?[/color]

What array?
[color=blue]
> for example
> array[i] = contains the value of {h, e, l, l, o}[/color]

How is 'array' declared?
[color=blue]
> I need to complare the entire value with something else not just the last
> value (o)
>
> array[i] == string;[/color]

Have you tried

string == array

?

Read FAQ 5.8. FAQ is here: http://www.parashift.com/c++-faq-lite/

Victor


Ron Natalie
Guest
 
Posts: n/a
#3: Jul 19 '05

re: stacks and array



"JC" <keepit@secret.com> wrote in message news:9_mdnXcr4foiqRmiU-KYiA@comcast.com...[color=blue]
> Hello,
> Can I convert the entire content of an array into a string?
>
> for example
> array[i] = contains the value of {h, e, l, l, o}[/color]

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

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




Thomas Matthews
Guest
 
Posts: n/a
#4: Jul 19 '05

re: stacks and array


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

Closed Thread