string.length() equivalent for arrays?

I'm writing a simple program to help me solidify what I've learned and to also give me opportunities to learn new things, anyways, is there a function I can call that will tell me how many places in an array are being used? kind of like the length() function for strings. By the way, the reason I'm using an array is so I can have my program modify each character in the array independently. I've tried googling it, but the closest thing I found that seamed to be what I was looking for turned out to be completely different from what I wanted.
also, if more info is needed, I'll gladly post it, I don't do a lot of forum posting and such, so when it comes to remembering to put certain info down, it's possible I could forget.
There is none. That's why you use strings or vectors instead.
how would I go about using strings would I be able to modify each character in the string independently? and also, what are vectors? and how do I use/declare them?
Last edited on
Have you tried using the [] operator with strings?

Vectors are like an array that can easily change its size as the program is running. You can find them in the <vector> header. Here is a comparison:

1
2
3
type name[non-variable_size]; //Static Array
type* name = new type[size]; //Dynamic Array
std::vector <type> name(size); //Vector 


You can access vectors' individual elements with [] just as you can with an array. In addition, they have some other handy functions:
1
2
3
name.push_back(element); //Adds a copy of element onto the end of the vector, expanding it by one.
name.resize(size); //Exactly what it says on the tin.
name.size(); //string.length() equivalent for vectors. 


Have fun!

-Albatross

Last edited on
thanks a ton

Edit: since I'm gonna need to do a little more research on vectors cause I don't think I have a full understanding of them, I figured out a quick fix to my problem, I just made a copy of the array into a string and then used the length() function from there...

1
2
3
4
5
6
7
8
    if(passp == 1){
        string spass(pass);
        passl = spass.length();
        for(int x = 0;x <= passl;x++){
            pass[x]=/*character modifier*/;
            cout<<pass[x];
        }
    }
Last edited on
I am not sure exactly about the question.
For a char array, can't you use strlen(), or just search for the null character at the end?
@Albatross
Have you tried using the [] operator with strings?

didnt see that part, no I havent tried that, Ill have to do that cause I want the user to be able to input strings into my program, but it seems an array won't cut it for what I want it to do, it keeps cutting off the storage of characters as soon as a space comes up in the input.

@xkcd83
I think strlen() only works on strings, I could be wrong, the most I know about strings is how to declare them/store a string in them and use length() to tell me the number of characters in them, I'll have to try it
I found a way to put user entered "spaces"(e.g. " ") into an array, I don't know if anyone else has thought of this(somebody probably has...) or if its even a good concept or anything, but I found it useful so I'll put the code here for someone else to possibly find, the user can define a string with spaces in it and this will convert it to an array so that you get the desired result instead of everything before the first space, just use getline() to assign the contents of the string and then replace fname and fname2 with the desired names, then when you can use the array(fname2) to whatever you need(I'm using it for assigning file names, since sometimes people want to name their file "my file.txt" instead of "myfile.txt")

1
2
3
4
5
6
    int x;
    x = fname.length();
    char fname2[x];
    for(int b = 0; b <= x; b++){
        fname2[b] = fname[b];
    }
Declaring an stack array with a variable number of elements like you are on line 3 is not legal C++. And you are going beyond the end of the array in your for loop anyway; you try to copy x+1 elements when you have declared fname2 to hold only x.

I don't see any reason to convert the string to an array every. If you happen to temporarily need a C-style string, use c_str().
EDIT: Sorry, I was thinking of something else. This will only work for strings.
Last edited on
@Zhuge
fname is a string that you must have previously declared and assigned a string, the 2nd line finds the number of characters used in the string "fname", because x is storing the number, when the character array fname2 is declared it makes the array the size of whatever x is at that time, the reason I wanted to convert the string to an array is because according to my compiler, I cant use a string in the same way I use a character array when using ofstream() to make and open files, as long as you have lines 1 and 2, it should be perfectly legal, the for loop just transfers each letter from the string to the correct spot in the array.

EDIT: And the reason I'm starting out with strings is so the user will be able to enter file names and text like "my file.txt" instead of being limited to file names like "myfile.txt", because if you use an array in the start it will cut off user input after the first space.
Last edited on
Use c_str() instead. Zhuge is right in that. They are essentially an array of character values. Read up on them since they will give you more of the functionality you are wanting from an array of characters. I even believe there is a length function associated with a c_str() but I'm not for sure on that one.
@ddwiters45
I'll read up on c_str(), thanks
Topic archived. No new replies allowed.