Given a string s, the task is to find the length of the string.
Examples:
Input: s = "abc"
Output: 3Input: s = "GeeksforGeeks"
Output: 13Input: s = ""
Output: 0
Try It Yourself
Using In-built methods
Every programming language offers a built-in method as well to find the length
// C++ program to find length
// of a string
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string s = "gfg";
cout << s.size() << endl;
return 0;
}
// C program to find the length of
// string using strlen function
#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "gfg";
printf("%ld", strlen(s));
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
class GfG {
public static void main(String[] args)
{
String s = "gfg";
System.out.println(s.length());
}
}
# Python code to demonstrate string length
# using len
s = "gfg"
print(len(s))
using System;
class GfG
{
static void Main()
{
string s = "gfg";
Console.WriteLine(s.Length);
Console.ReadLine();
}
}
let s = "gfg";
console.log(s.length);
Output
3
Programming Language | In-Built method to find the length of the string |
|---|---|
C | |
C++ | |
Java | |
Python | |
JavaScript | |
C# |
Writing your Method
The most traditional method of finding the length of the string is by traversing each character through the loop.
- Using a counter, traverse each character of the string with the help of Loop.
- Return the counter value as the length of the string.
#include <bits/stdc++.h>
using namespace std;
// Method to calculate length of a string
int getLength(const string& s) {
int i = 0, cnt = 0;
while (s[i]) {
i++;
cnt++;
}
return cnt;
}
int main() {
string s = "GeeksforGeeks";
cout << getLength(s) << endl;
return 0;
}
#include <stdio.h>
// Function to calculate length of a string
int getLength(const char* s) {
int i = 0, cnt = 0;
while (s[i] != '\0') {
i++;
cnt++;
}
return cnt;
}
// Driver code
int main() {
const char* s = "GeeksforGeeks";
printf("%d\n", getLength(s));
return 0;
}
public class GfG {
// Method to calculate length of a string
static int getLength(String s) {
int cnt = 0;
for (char c : s.toCharArray()) {
cnt++;
}
return cnt;
}
public static void main(String[] args) {
String s = "GeeksforGeeks";
System.out.println(getLength(s));
}
}
# Function to calculate length of a string
def get_length(s):
cnt = 0
for c in s:
cnt += 1
return cnt
# Driver code
s = "GeeksforGeeks"
print(get_length(s))
using System;
class Program
{
// Method to calculate length of a string
static int GetLength(string s)
{
int cnt = 0;
foreach (char c in s)
{
cnt++;
}
return cnt;
}
static void Main()
{
string s = "GeeksforGeeks";
Console.WriteLine(GetLength(s));
}
}
// Method to calculate length of a string
function getLength(s) {
let i = 0, cnt = 0;
// Use a while loop to iterate through
// the characters until the end of the string
while (s[i] !== undefined) {
i++;
cnt++;
}
return cnt;
}
// Driver Code
let s = "GeeksforGeeks";
console.log(getLength(s));
Output
13
Time Complexity: O(n), where n is the length of the string.
Auxiliary space: O(1)