Answers for "length of string basic"

C
0

String Length

//Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object. 

Example
public class StringDemo {

   public static void main(String args[]) {
      String palindrome = "Dot saw I was Tod";
      int len = palindrome.length(); // Using the length method
      System.out.println( "String Length is : " + len );
   }
}
Posted by: Guest on August-31-2021
0

String length program

#include<stdio.h>

int main()
{
    //let's assume the maximum string size as 100.
    //initialize length as 0. Otherwise, it will take some garbage value.
    char str[100];
    int i, length = 0;
    
    //Get string input from the user
    printf("Enter the stringn");
    scanf("%s",str);
    
    /*Loop through each character 
    and increment the length till we reach the null character. */
    for(i = 0; str[i] != ''; i++)
          length++;
    
    //print the length
    printf("Length = %dn",length);
    
    return 0;
}
Posted by: Guest on September-03-2021

Code answers related to "C"

Browse Popular Code Answers by Language