# Determine the number of elements in a string array

Page 1 of 1

## 3 Replies - 1587 Views - Last Post: 18 February 2013 - 06:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312638&amp;s=9eb0e30e252fe1f86a048b3726cc802d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 NoRemorse

Reputation: 0
• Posts: 65
• Joined: 26-January 11

# Determine the number of elements in a string array

Posted 18 February 2013 - 01:37 PM

Letīs say that we have string stringArray[5] = {"Lucy","Mary","Tina"}
Is there a way to get the number of names in the array, not the size of the array nor the number of elements there can be in the array max? If there is not such a built-in way,can anyone please give me instructions how could I determine that number? Any help is appreciated.
Is This A Good Question/Topic? 0

## Replies To: Determine the number of elements in a string array

### #2 BetaWar

• #include "soul.h"

Reputation: 1560
• Posts: 8,349
• Joined: 07-September 06

## Re: Determine the number of elements in a string array

Posted 18 February 2013 - 01:48 PM

With a standard array there is no built in way to get the number of elements in it. However, if you are willing to NULL fill the array (or otherwise fill the array with a known non-value) you can loop through it and determine how many of the values are not the known non-value.

For instance:
```string stringArray[5] = {"Lucy", "Jill", "Amy", "", ""}; // where "" is the known non-value
int numberOfElements = 0;
for(int i = 0; i < 5; i++){
if(stringArray[i] == ""){
numberOfElements++;
}
}
cout << "Elements: " << numberOfElements << endl;

```

Hope that helps.

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3803
• Posts: 13,808
• Joined: 08-August 08

## Re: Determine the number of elements in a string array

Posted 18 February 2013 - 01:51 PM

Since you're using C++ strings, why not use C++ vectors? Then you could just do:
```cout << "Elements: " << stringArray.size() << endl;

```

### #4 #define

• Duke of Err

Reputation: 1855
• Posts: 6,678
• Joined: 19-February 09

## Re: Determine the number of elements in a string array

Posted 18 February 2013 - 06:29 PM

The default constructor creates an empty string so null values should not be required.