From 3a68ac260a1374fe8b61de3c2c9700169c16df3a Mon Sep 17 00:00:00 2001 From: Sambhav Kaul Date: Sun, 14 Oct 2018 23:12:58 +0530 Subject: [PATCH] Update length_string using loop --- programs/C/length_string.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/programs/C/length_string.c b/programs/C/length_string.c index 622a838..1f3baf5 100644 --- a/programs/C/length_string.c +++ b/programs/C/length_string.c @@ -1,26 +1,26 @@ #include -#include //Created a function to calculate length of string. int LengthOfString(char string[50]) { + int length = 0, i; + scanf("%[^\n]" , string); //Input of the string - scanf("%[^\n]" , string); //Input of string with blank spaces - int length = strlen(string); + for(i=0; string[i]!='\0'; i++) + { + length++; + } return length; } - int main(void) - { char string [50]; int length; length = LengthOfString(string); // Using the function - - printf("Length %d \n" , length); // Printing the length + printf("%d \n" , length); // Printing the length return 0; -} \ No newline at end of file +}