Finding the length/size of a string

To find the length of the String, we need:

  • A string for which the length/size is to be determined (say “str”)

Below is the implementation of the above approach:

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 = "w3wiki";

    // size of string object using size() method
    cout << str.size() << endl;

    return 0;
}
C
#include <stdio.h>
#include <string.h>

int main()
{
    // String
    char str[] = "w3wiki";

    // Length of string using strlen() function
    int length = strlen(str);

    printf("%d\n", length);

    return 0;
}
Java
public class Main {
    public static void main(String[] args)
    {
        // String object
        String str = "w3wiki";

        // Size of string object using length() method
        System.out.println(str.length());
    }
}
// This code is contributed by Utkarsh
Python
# Main function
def main():
    # String object
    str = "w3wiki"

    # Size of string object using len() function
    print(len(str))


# Calling the main function
if __name__ == "__main__":
    main()
JavaScript
// JavaScript program to find length
// of a string

// String
let str = "w3wiki";

// size of string using length property
console.log(str.length);

Output
13

Basic String Operations with Implementation

In this post, we will look into some of the basic String operations such as:

  • Accessing characters by index in a string.
  • Inserting character into a String.
  • Modifying character in String
  • Deletion of Character in String
  • Concatenating strings (combining multiple strings into one).
  • Finding the length of a string
  • Comparing strings for equality or lexicographical order

Let us consider the basic String operations one by one.

Similar Reads

Accessing characters by index in a string.

To access any character in a String, we need:...

Inserting Character/String into an String.

To insert any Character/String in a String, we need:...

Modifying character in String

To modify any Character in a String, we need:...

Deletion of character in String

To delete any Character in a String, we need:...

Concatenating strings (combining multiple strings into one).

To concatenate any String to a String, we need:...

Finding the length/size of a string

To find the length of the String, we need:...

Comparing Strings for Equality

To compare strings, Define a function to compare values with the following conditions :...

Contact Us