Length of a String Last Updated : 04 Jun, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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: 0Using In-built methodsEvery 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); Output3 Programming LanguageIn-Built method to find the length of the stringCstrlen()C++size()Javalength()Pythonlen()JavaScriptlengthC#length()Writing your MethodThe 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)); Output13 Time Complexity: O(n), where n is the length of the string.Auxiliary space: O(1) Comment More infoAdvertise with us Next Article How to Find Length of String in Bash Script? S srinam Follow Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads Batch Script - String length In this article , we are going to learn how to find length of any String using Batch Script. Batch Script :@echo off set str=Geeks For Geeks call :strLen str strlen echo String is %strlen% characters long pause exit /b :strLen setlocal enabledelayedexpansion :strLen_Loop if not "!%1:~%len%!"=="" set 2 min read Length Of Last Word in a String Given a string s consisting of upper/lower-case alphabets and empty space characters ' ', return the length of the last word in the string. If the last word does not exist, return 0. Examples: Input : str = "Geeks For Geeks"Output : 5length(Geeks)= 5Input : str = "Start Coding Here"Output : 4length( 13 min read Longest substring of vowels Given a string s of lowercase letters, we need to find the longest substring length that contains (a, e, i, o, u) only. Examples : Input: s = "geeksforgeeks" Output: 2 Longest substring is "ee" Input: s = "theeare" Output: 3 The idea is to traverse the string and keep track of the current number of 5 min read How to Find Length of String in Bash Script? In this article, we are going to see how to get string length in Bash scripting. Here are a few ways from which we can get the length of the string in BASH: Using the # operatorUsing while loopUsing expr commandUsing awk commandUsing wc command Using these mentioned tools and commands, we will be ab 5 min read How to limit string length in PHP ? A string is a sequence of characters in PHP. The length of the string can be limited in PHP using various in-built functions, wherein the string character count can be restricted. Table of ContentUsing for loopUsing mb_strimwidth() function Using substr() method Using Regular Expressions with preg_m 6 min read Using LENGTH() Function in SQL Understanding the length of strings within a database is a fundamental aspect of effective data management and analysis. In the world of SQL, the LENGTH() function provides an essential tool for measuring the number of characters in a string, which can be invaluable for various tasks such as data va 4 min read Like