Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIFind the length of a String

Find the length of a String

Given a string str, the task is to find the length of the string.

Examples:

Input: str = “Geeks”
Output: Length of Str is : 5

Input: str = “neveropen”
Output: Length of Str is: 13

Approach 1: Iterative(using Loop)

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.
  • Update the counter for every character
  • When the string is terminated or a null character is identified, break the loop.
  • Return the counter value as the length of the string.

Below is the implementation of the above method:

C++




// C++ program to find length
// of a string
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
 
    // String obj
    string str = "neveropen";
 
    // The constructor of string will set
    // it to the C-style string,
    // which ends at the '\0'
 
    // size of string object Using while loop
 
    // while 'NOT NULL'
    int i = 0, cnt = 0;
    while (str[i]) {
        i++;
        cnt++;
    }
    cout << cnt << endl;
 
    return 0;
}


C




// C program to find the length of string
#include <stdio.h>
#include <string.h>
 
int main()
{
    char Str[] = "neveropen";
    int i = 0, cnt = 0;
 
    while (Str[i]) {
        cnt++;
        i++;
    }
 
    printf("%d", cnt);
 
    return 0;
}


Java




public class StringLength {
    public static void main(String[] args) {
        // String object
        String str = "neveropen";
 
        // Initialize a variable to count the characters
        int i = 0, cnt = 0;
 
        // Use a while loop to iterate through the characters until the end of the string
        while (i < str.length()) {
            i++;
            cnt++;
        }
 
        // Print the length of the string
        System.out.println(cnt);
    }
}


Python3




# Python program to find length
# of a string
 
# String obj
str = "neveropen"
 
# size of string object Using while loop
 
i = 0
cnt = 0
while str[i:]:
    i += 1
    cnt += 1
print(cnt)


C#




using System;
 
class Program {
    static void Main(string[] args) {
        // String object
        string str = "neveropen";
 
        // Use the Length property to get the length of the string
        int length = str.Length;
 
        // Print the length of the string
        Console.WriteLine(length);
    }
}


Javascript




// String object
let str = "neveropen";
 
// Initialize a variable to count the characters
let i = 0, cnt = 0;
 
// Use a while loop to iterate through the characters until the end of the string
while (str[i] !== undefined) {
    i++;
    cnt++;
}
 
// Print the length of the string
console.log(cnt);


Output

13







Time Complexity: O(N), where N is the length of the string.
Auxillary space: O(1)

Approach 2: Using In-built methods

Every programming language offers a built-in method as well to find the length of the string, such as:

Programming Language

In-Built method to find the length of the string

C

strlen()

C++

size()

Java

length()

Python

len()

Javascript

length

C#

length()

Below is the implementation of the above methods:

C++




// C++ program to find length
// of a string
#include <iostream>
#include <string.h>
using namespace std;
 
// Driver code
int main()
{
    // String obj
    string str = "neveropen";
 
    // size of string object using size() method
    cout << str.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 Str[] = "neveropen";
 
    printf("%ld", strlen(Str));
 
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        String str = "neveropen";
        int stringSize = str.length();
        System.out.println(stringSize);
    }
}


Python




# Python code to demonstrate string length
# using len
 
str = "neveropen"
print(len(str))


C#




using System;
 
class Program
{
    static void Main()
    {
        // String variable
        string str = "neveropen";
 
        // Length of the string using the Length property
        Console.WriteLine(str.Length);
 
        // Alternatively, you can also use the String.Length method:
        // Console.WriteLine(str.Length);
 
        // Pause the program execution to see the result
        Console.ReadLine();
    }
}
//Contributed by Aditi Tyagi


Javascript




// String object
let str = "neveropen";
 
// Use the `length` property of the string object to get its length
// The `length` property directly gives the length of the string
let length = str.length;
 
// Print the length of the string
console.log(length);


Output

13







Time Complexity: O(1), except strlen() for C it is O(N)
Auxillary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments