hi everyone, question strlen gives me the length of a string exemple strlen tom will be three, but how do i output the character size of the array str[12]
hi everyone, question strlen gives me the length of a string exemple strlen tom will be three, but how do i output the character size of the array str[12]
do you mean, how do you check how many characters are in str? You'd know that from the declaration, it's a part of the datatype. Alternatively, you can use the sizeof operator, IE
sizeof( str );
If you are working with a pointer to the first element and want to check the length of the array itself, for instance, if you dynamically allocated the array, then you can't check the array length -- you'd have to manually keep track of it when you allocate it.
strlen takes a const char* as argument and counts the characters until it hits a null-terminator.
If I don't got you wrong now, maybe you want to do somethilng like this ? :
z would now be 3...Code:char bla[12]="hej"; int z = strlen((char*)bla);
hope this helps
/btq
...viewlexx - julie lexx
The typecast is unecissary as the value of bla in an expression IS a pointer to a charOriginally posted by btq
Code:char bla[12]="hej"; int z = strlen((char*)bla);
strlen( bla );
would work exactly the same way.
thanks a lot Polymorphic OOP!
that was esactly what i was looking for, it worked just fine,thanks again.
yeah, hum...I thought that was strange and unneeded. I got a compiler error before when doing that so I threw in the (char *) and the error went away...now it has dissapeared though.The typecast is unecissary as the value of bla in an expression IS a pointer to a char
strlen( bla );
would work exactly the same way.
Ahh well..
/btq
...viewlexx - julie lexx
hi and thanks for your rsponces ,but im stock again at the nd of my progrand, waht i need to do is concanate first 2 characters of one string to another, it does it fine but i get a whole bunch of other character at the end of it plus the same string again, how do i stop that from happening here is some of my code, i have included <cstring>
im entering billy for string 1Code:int main() { char string1[10]; char string2[15]; char string3[15]; char string4[8]; cout <<"Enter string1: "; cin.getline(string1, 10, '\n'); cout <<"Enter string2: "; cin.getline(string2, 15, '\n'); cout<<endl<<string1<<" is "<<strlen(string1)<<" characters in length"<<endl; cout<<string2<<" is "<<strlen(string2)<<" characters in length"<<endl; cout<<endl<<string1<<" is "<< sizeof(string1) << " characters in size"<<endl; cout<<string2<<" is "<< sizeof(string2) << " characters in size"<<endl; if (strcmp(string1,string2) !=1 || 0) { cout<<endl<<string1<<" is less than "<<string2<<endl; } cout<<endl<< "string3 after concatenation is "<<strcat(string1,string2)<<endl; cout<<endl<<"String 4 is " <<strncpy(string2,string1,3)<<endl; cout<<endl<<"String 1 is now " <<strncpy(string2, string1,7)<<endl; return 0; }
bob for string 2