Find the length of the string without using the inbuilt function

Problem Statement:- Program to Find the length of the string without using the inbuilt function.

Sample Input/Output:-


Sample Input First:

csinfo 360

Sample Output First: 

10

Sample Input Second: 

Coronavirus 2020

Sample Output Second: 

16

Data requirement:-

   Input Data:- str

  Output Data:-len

Program in C

Here is the source code of the C Program to Find the length of the string without using the inbuilt function.

Code:

#include<stdio.h>
int main()
{
    char str[30];

    //Get the Input String
    printf("Enter the String:");
    scanf("%[^\n]",str);
    int len=0;

    //calculate string size
    while(str[len]!='\0')
    {
        len++;
    }

//display the string total length
    printf("Your Enter String length is:%d",len);
}

Input/Output:
Enter the String:csinfo 360
Your Enter String length is:10

Program in C++

Here is the source code of the C++ Program to find the length of a string without using the library function.

Code:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    cout<<"Enter the String:";
    getline(cin,str);
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    cout<<"Your Enter String length is: "<<len;
}

Input/Output:
Enter the String:csinfo 360 com
Your Enter String length is: 14

Program in Java

Here is the source code of the Java Program to Find the length of the string without using the inbuilt function.

Code:

import java.util.Scanner;
public class p3 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
str+='\0';
        int len=0;
        while(str.charAt(len)!='\0')
        {
        len++;
        }
System.out.println("Your Enter String is: "+len);
cs.close();
}
}

Input/Output:
Enter your String:
Coronavirus 2020
Your Enter String is: 16

Program in Python

Here is the source code of the Python Program to find the length of a string without using the library function.

Code:

str=input("Enter the String:")
len=0
while str[len:]:
    len+=1
print("Your Enter String is:", len)

Input/Output:
Enter the String:COVID-19 COVID-19
Your Enter String is: 17



Post a Comment

0 Comments