Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I check the size of string

+5 votes
asked Jun 23, 2022 by slavik kozakov (210 points)

6 Answers

+3 votes
answered Jun 25, 2022 by Peter Minarik (84,720 points)

What language are you after?

For C strings, you use the strlen() function.

In C++, you have a length() function on your std::string type.

Most object-oriented languages have a property on the string called Length or something similar that return the length.

In Python, it's the len() function.

0 votes
answered Jul 4, 2022 by Ankit Singh (140 points)

For checking the size of string you can use sizeof operator. In C language their is no such thing called string but you can treat character array as a string.

Program :

#include<stdio.h>
int main(){
char *a = "America";    // Here, a is character pointer
char b[] = "German";    // Here, b is character array   
printf("size of a = %ld\n",sizeof(a));
printf("size of b = %ld\n",sizeof(b));
}

Output :

size of a = 8
size of b = 7

Link : Code

NoteHere extra 1 size due to NULL '\0' and size of operator never evaluates the result it just gives the sizeof the result.

0 votes
answered Jul 5, 2022 by MadhuGowthaman (190 points)

Hi,

In Java, you can use the inbuild java function of .length() to get the size of the string.

public class Main

{

public static void main(String[] args) {

    String str = "sample";

    int strLength = str.length();

System.out.println("strLength length is::"+strLength);

}

}

strLength length is::6

0 votes
answered Jul 7, 2022 by UTKARSH TRIPATHI (140 points)
public class Main

{

public static void main(String[] args) {

    String str = "sample";

    int strLength = str.length();

System.out.println("strLength length is::"+strLength);

}

}
0 votes
answered Jul 9, 2022 by Saurabh Kumar (140 points)

n=input("Enter a String: ")

print(len(n))

0 votes
answered Jul 12, 2022 by praveen kumar (140 points)
use len() method in python
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...