Find out the string length without using strlen() function and any loop. | C Programming and Computer Geeks

Search MY Blog

Wednesday, 1 January 2014

Find out the string length without using strlen() function and any loop.


We are having many methods to find the string length for a given string, like implementing the strlen() functionality using loop, etc..

Here in the below program, we are going to see some interesting logic which can give us the string length..

#include "stdlib.h"
#include "stdio.h"

int main()
{

//string length without using strlen() and loop.
     char str[150] = {0,};
     int length = 0;

     printf("Please enter a string:\n");
     gets(str);

     printf("\n\n");

     length = printf("%s", str);
     printf("\n\nLength of entered String is = %d", length);

     getch();
     return 0;
}

Output:

C Program for String length with out using strlen() function and loop
String length by printf()

Logic: We all know that printf() function returns the number of characters displayed(or printed ) on output screen. So using this logic we have found the string length for the entered string.


Read more about basics of Input/output functions.

Hope this Post recalled your basics once again..

Please share it with all your friends.

Post comments for any clarification and doubts. And also share your ideas/opinions as well.

1 comment:

Search This Blog