C++ length of a string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

C++ length of a string

Is there a way I can get the length of c++ string? (Purpose is so I can then copy the characters to another array)

29th Oct 2018, 9:42 AM
NipIsTrue
6 Answers
+ 3
if you are using C++, not C, you should be able to use std::string which has method length().
29th Oct 2018, 10:17 AM
Jakub Stasiak
Jakub Stasiak - avatar
30th Oct 2018, 5:49 AM
shubham
+ 4
// Use this function: int lengthOf(string str) { int len = 0; while(str[len]) { // Gets the character at len index. If it is there, then it will return a // character, which is true in boolean, but no character // (aka end of string) will result in undefined, which is false. len++; } return len; }
30th Oct 2018, 9:14 AM
Rowsej
Rowsej - avatar
+ 2
or if you're using char array char s[]="hello"; cout<<sizeof(s)/sizeof(s[0]); //6, because it end with \0
29th Oct 2018, 9:52 AM
Taste
Taste - avatar
+ 1
you can use strlen() to find string length...but to copy the string you don't need to use strlen(),,,you can use strcpy() to copy the string
29th Oct 2018, 11:13 AM
1704086_ibnul
1704086_ibnul - avatar
0
For null terminated strings that originate in other functions you have strlen()
29th Oct 2018, 10:06 AM
Vlad Serbu
Vlad Serbu - avatar