Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find length of a string without string.h and loop in C?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 856
    Comment on it

    Program to find length of a string without string.h and loop in C?

    #include <stdio.h>
    
    int main()
    {
         char s100];
         printf( "\rLength is: %d", printf("Entered string is: %s\n", gets(s)) - 20);
    
         return 0;
    }
    

    output:

    Entered string is: FirstDemo
    Length is: 9

    Explanation:

    gets() function in C reads characters from the standard input (stdin) and stores them as a C string into s until a newline character or the end-of-file is reached. In this program printf function is called from within printf. The second printf() is calling gets() function and prints the entered string using returned value of gets(). When a printf is called from another printf then inner printf returns number of characters successfully printed. Since, it also prints 20 extra characters for printing Entered string is: and \n. This is the reason 20 is subtracted from the returned value of second printf and get the length.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: