This blog is under construction

Monday 24 June 2013

C program to find the length of the given string without using strlen

Write a C program to find the length of the given string without using library function(strlen).


  #include <stdio.h>
  int main() {
        char str[100], len = 0, i = 0;

        /* get the input string from the user */
        printf("Enter your input:");
        fgets(str, 100, stdin);

        /* find the length of the given string */
        while (str[i] != '\n' && str[i] != '\0') {
                len++;
                i++;
        }
        str[i] == '\0';

        /* print the length of the given string */
        printf("Given string length:%d\n", len);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:Hello World
  Given string length:11



No comments:

Post a Comment