Program to Calculate Length of a String (without using strlen in-built function).

#include
#include
void main(){
char *str;
int count = 0,i;
clrscr();
printf("\nEnter the String: ");
gets(str);
for(i=0;str[i]!='\0';i++){
count++;
}
printf("\nThe length of the string is %d.",count);
getch();
}

Output:
Enter the String: Learn C Online
The length of the string is 14.

You may also like...

30 Responses

  1. Anonymous says:

    In ‘\0’ condition the program is not terminating,,its counting ‘\0’ also and giving the result

  2. Anonymous says:

    how to count the word
    give pgm for me

  3. Anonymous says:

    in calculating the length of the string do ‘\0’ also counts????

  4. instead of char *str you must put char str[ ]

  5. Anonymous says:

    is it ok to replace get with scanf?

    • Chetan Singh Sengar says:

      Ya it is Ok but you can mostly use the gets because of it is easy way to scan a string 😀

  6. “clrscr();” what is this?
    is this true?

    • Subhajit Bhattacharjee says:

      clrscr(); ::is a function which is mainly used for clear the output screen..if you dont write it in your program then the output screen also show your previous output…

  7. Anonymous says:

    nice

  8. Venkat D says:

    Word Count Program

    #include
    #include
    void main(){
    char *str;
    int count = 0,i;
    int WordCount=1;
    clrscr();
    printf(“\nEnter the String: “);
    gets(str);
    for(i=0;str[i]!=’\0′;i++){
    count++;
    if(str[i]==’ ‘)
    WordCount++;

    }
    printf(“\nThe length of the string is %d.”,count);
    Printf(“WordCount is:%d”,WordCount);
    getch();
    }

    Output : Enter The String: Hi Hello Everybody
    lenth of the string is :19
    WordCount is: 3

  9. Anonymous says:

    #include
    #include
    void main()
    {
    int count=0,i=0;
    char str[20];
    clrscr();
    printf(“Enter the string:”);
    scanf(“%s”,str);
    while(str[i]!=’\0′)
    {
    count++;
    i++;
    }
    printf(“The string length is:%d”,count);
    getch();
    }

  10. Anonymous says:

    kindly give me a program in string function that could tell if the word is a palindrome.. 🙂

    • Chetan Singh Sengar says:

      #include
      #include
      #include
      void main()
      {
      char *st,*ptr;
      int i;
      clrscr();
      printf(“enter the string to scan : “);
      gets(st);
      strcpy(ptr,st);
      strrev(st);
      i=strcmp(ptr,st);
      if(i==0)
      printf(“Entered string is palindrome…”);
      printf(“Entered string is not palindrome…”);
      getch();
      }

  11. clrscr();
    is clear the screen before all of the items be printed off or display

  12. bonny says:

    #include
    #include
    void main(){
    char *str;
    int len,i;
    clrscr();
    printf(“\nEnter the String: “);
    gets(str);
    for(i=0;str[i]!=’\0′;i++){
    len=i;
    }
    printf(“\nThe length of the string is %d.”,len);
    getch();
    }

    will this one work fine???

  13. no it will print (n-1) characters ,becoz u taking ‘i’ value initiate with ‘0’ so it will display of length (length-1)

  14. Abin Rimal says:

    Instead why can’t we use sizeof() function,
    like
    #include
    #include
    #include
    void main()
    {
    char str[]=”this is the string time”;
    cout<<sizeof(str);
    }

  15. DIPU ANAND PATEL says:

    write a c program to sort a list of string according to the length of the string in descending order using a two dimensional array. The program will continue accepting string until and unless “END” is typed.The program should have two functions i.e main and another reorder(to arrange the list of string). you cannot use any predefined string functions such as strcmp or strcpy or even strlen.

  16. Chetan Singh Sengar says:

    Sir is that possible that we can make a program which print itself in the function ? :/ :/

  17. Chetan Singh Sengar says:

    Sir i can’t understand the Malloc() & Calloc() is there anyone who tells me abot them with example in a easy way ? :/ :/

  18. aditya says:

    hey i want to make a program in c in which it takes input as a string but print string without spaces?

  19. lasheena lachu says:

    guyz plzz somebody help me to find the sqare of first n natural no……. 🙂 ::

  20. Matt says:

    thanks very interesting keep up the amazing work thank you here is a superb weblog

  21. Prakasha jency says:

    I want a program for finding maximum length in my given string.. Guys help me with that ..
    If my input is ant leaves elephant
    I need to get the output as elephant

  1. May 4, 2015

    […] Calculate Length of a String (without using strlen in-built function) […]

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet