Arrays, strings.

closed account (LzqpfSEw)
There's several questions here:

First of all,
1
2
char Arrayname[Arraynum];
Arrayname[0] = 'abc';


That doesn't work for obvious reasons, char only holds one character. What can I use so it can hold words (I've tried string that doesn't work).

Second,

How can I add a word to a string without replacing the whole thing.

1
2
string Stringname;
Stringname = 'abc';


Assigns 'abc' to Stringname, how can I add 'xyz' to the string without replacing the previous 'abc'.

Lastly,

I'm sending 10 random words to a file, but since it's random usually 2 words pop up twice, how can I send words to a file but not send it if that word has already been sent.

I've tried quite a few things to do this but nothing has seemed to work so far.

___

I'm not in a programming class, don't worry about doing my homework. Just hand me some code snippets and I'll be just fine.
Last edited on
Ok, let me tackle this one question at a time:

In C++ a string is an array of characters. Using strings is how you hold words.

1
2
3
#include<string>
...
string Stringname;


To reference the first character for instance you can use:

 
Stringname[0]


Or try:

 
Stringname.at(0);


Second question:
The + operator acts to concatenate strings.

1
2
3
Stringname = "abc";
Stringname = Stringname + "xyz";
cout << Stringname;


The output would be "abcxyz."

The third question:
You can use a nested for loop with an if statement.

1
2
3
4
for (int i = 0; i <= Stringname.size(); i++)
   for (int j = 0; j < = Stringname.size(); j++)
      if (Stringname[i] == Stringname[j])
         ...
Topic archived. No new replies allowed.