Skip to content

Commit 3a68ac2

Browse files
committed
Update length_string using loop
1 parent 7690164 commit 3a68ac2

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

programs/C/length_string.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#include <stdio.h>
2-
#include <string.h>
32

43
//Created a function to calculate length of string.
54
int LengthOfString(char string[50])
65
{
6+
int length = 0, i;
7+
scanf("%[^\n]" , string); //Input of the string
78

8-
scanf("%[^\n]" , string); //Input of string with blank spaces
9-
int length = strlen(string);
9+
for(i=0; string[i]!='\0'; i++)
10+
{
11+
length++;
12+
}
1013

1114
return length;
1215
}
1316

14-
1517
int main(void)
16-
1718
{
1819
char string [50];
1920
int length;
2021

2122
length = LengthOfString(string); // Using the function
22-
23-
printf("Length %d \n" , length); // Printing the length
23+
printf("%d \n" , length); // Printing the length
2424

2525
return 0;
26-
}
26+
}

0 commit comments

Comments
 (0)