GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C++ String Length
Selected Reading
C++ String LengthLength of a string is the number of characters present in the string. These characters can be of the data type char, and these include all alphanumeric elements, symbols and miscellaneous characters. In C++ programming language, there are two types of strings- Character Arrays of C-Style type, and String objects which are built-in objects of <string> class. The length of a string also includes white spaces, but in case a string includes a terminating character "\0", the string ends at that character and the count of length is terminated just before that character. There are many ways to find the length of a given string. Some of these methods are iterative, whereas some also use in-built functions and methods. These methods are explained clearly in the following parts of this chapter −
String Length Using strlen() MethodStrings are defined as character arrays which are accessed using the pointer to the first iterator of the array. We can use strlen() method of C Library to calculate the length of C-type arrays. SyntaxThe following syntax shows how to use strlen() method to calculate the length of the string − strlen(string_name); ExampleThe following example shows how to calculate the length of the string using strlen() method − #include <bits/stdc++.h>
using namespace std;
int main() {
char s[]="I love TP !!!";
cout<<"Length of string s : "<<strlen(s);
return 0;
}
OutputLength of string s : 13 String Length Using string::size() MethodMost programmers commonly use string::size() method of string class when there is a need to calculate the length of a string in C++ programming language. It is the most basic method, and it is generally used while traversing a string object. SyntaxThe following syntax shows how to use size() method to calculate the length of the string − string_object.size(); ExampleThe following example shows how to calculate the length of the string using size() method − #include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
cout<<"Length of string s : "<<s.size();
return 0;
}
OutputLength of string s : 13 String Length Using string::length() MethodWe can also use length() method of string class to determine the length of the given string. Both length() and size() methods are part of <string> header file, and these are called as methods to the string object. SyntaxThe following syntax shows how to use length() method to calculate the length of the string − string_object.length(); ExampleThe following example shows how to calculate the length of the string using length() method − #include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
cout<<"Length of string s : "<<s.length();
return 0;
}
OutputLength of string s : 13 String Length Using while LoopWe can use a simple while loop to iterate over the string and initialize a variable count to calculate the length of the string until we reach the end of the string. For each iteration, the count increases by one, hence the net result will be the length of the string. SyntaxThe following syntax shows how to use a while loop to calculate the length of the string − while(s[i]!='\0') {
[body]
}
ExampleThe following example shows how to calculate the length of the string using a single while loop − #include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
int count=0, i=0;
while(s[i]!='\0')
count++, i++;
cout<<"Length of string s : "<<count;
return 0;
}
OutputLength of string s : 13 String Length Using a for LoopWe can use a simple for loop to iterate over the string and initialize a variable count to calculate the length of the string until we reach the end of the string. For each iteration, the count increases by one, hence the net result will be the length of the string. SyntaxThe following syntax shows how to use a for loop to calculate the length of the string − for(int i=0;s[i]!='\0';i++){
[body]
}
ExampleThe following example shows how to calculate the length of the string using a single for loop − #include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
int count=0;
for(int i=0;s[i]!='\0';i++)
count++;
cout<<"Length of string s : "<<count;
return 0;
}
OutputLength of string s : 13 Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |