Given a string s, the task is to find the length of the string.
Examples:
Input: s = "abc"
Output: 3
Input: s = "GeeksforGeeks"
Output: 13
Input: s = ""
Output: 0
Using In-built methods
Every programming language offers a built-in method as well to find the length
C++ // 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 // 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; } Java /*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 # Python code to demonstrate string length # using len s = "gfg" print(len(s))
C# using System; class GfG { static void Main() { string s = "gfg"; Console.WriteLine(s.Length); Console.ReadLine(); } } JavaScript let s = "gfg"; console.log(s.length);
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.
C++ #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; } C #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; } Java 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)); } } Python # 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))
C# 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)); } } JavaScript // 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)); Time Complexity: O(n), where n is the length of the string.
Auxiliary space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem
My Profile