The length of a string can be determined using the built-in library function strlen() in the “string.h” header file.
Examples:
String: “Hello”
Length of the string is: 5
String: “Sanfoundry”
Length of the string is: 10
Write a C Program to find the length of a String.
1. Take a string as input and store it in the array.
2. Using for loop count the number of characters in the array and store the result in a variable.
3. Print the variable as output.
There are several ways to find the length of the string in C language. Let’s take a detailed look at all the approaches to find the length of a string in C.
- Length of the String in C without using Built-in Function
- Length of the String in C using While Loop
- Length of the String in C using Strlen Function
- Length of the String in C using Recursion
In this approach we use the iterative statement to find the length of string using for loop. We start from the first character and traverse the whole string until we encounter the backspace character (‘\0‘), which marks the end of the string.
Example:
String: “Hello”
The actual representation of this string is: [ ‘H’ ,’e’ , ‘l’ , ‘l’ , ‘o’ , ‘\0’ ]
We traverse until a backspace character is encountered, So Length of the string is 5.
Here is source code of the C program to find the length of a string without using the built-in function.The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the length of a string without using the
* built-in function
*/
#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
// input the string
printf("Enter the string: \n");
gets(string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}
1. Take a string as input and store it in the array string[].
2. Using for loop count the number of characters in the array string[]. Use the variable length to keep the count of the characters in the array.
3. Do step-2 till the end of input string.
4. Print the variable length as output.
Time Complexity: O(n)
In the for loop we are iterating over the string until we encounter the backspace character, so time complexity is O(n), where n is length of string.
Space Complexity: O(n)
Space is required to store a sting, so the space complexity of string length program is O(n).
In this case, we enter the string “Hello” as input to find the length of the string.
Enter a string: Hello The length of a string is the number of characters in it So, the length of Hello = 5
In this approach, we use the iterative statement to find the length of string using while loop. We start from the first character and traverse the whole string until we encounter the backspace character (‘\0‘), which marks the end of the string.
Example:
String: “Sanfoundry”
The actual representation of this string is: [ ‘S’ ,’a’ , ‘n’ , ‘f’ , ‘o’ , ‘u’ , ‘n’ , ‘d’ , ‘r’ , ‘y’ ,‘\0’ ]
We traverse until a backspace character is encountered, So Length of the string is 10.
Here is source code of the C program to find the length of a string without using the built-in function.The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the length of a string using the while loop
*/
#include <stdio.h>
int main ()
{
int length=0;
char string[30];
// input the string
printf ("Enter the string: \n");
gets (string);
//Traversing until a backspace character is encountered which mark end of string
while (string[length] != '\0')
length ++;
printf("Length of string is: %d",length);
return 0;
}
1. Take a string as input and store it in the array string[].
2. Initialize length variables as 0.
3. Traverse the string until the backspace character is encountered and in each step increment the length variable.
4. Print the Length of the string.
Time Complexity: O(n)
In the for loop we are iterating over the string until we encounter the backspace character, so time complexity is O(n), where n is length of string.
Space Complexity: O(n)
Space is required to store a sting, so the space complexity is O(n).
In this case, we enter the string “Sanfoundry” as input to find the length of the string.
Enter the string: Sanfoundry Length of string is: 10
The best way to find the length of a string is using the built-in library function i.e. strlen()
strlen function:
Function Prototype: int strlen(const char *s1)
Return Value: returns the length of string
Example:
Input: String = “Sanfoundry Programs”
Output: Length of string is: 19
Here is source code of the C program to find the length of a string using strlen function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the length of a string using strlen function
*/
#include <stdio.h>
#include <string.h>
int main ()
{
char string[30];
// input the string
printf ("Enter the string\n");
gets (string);
// Storing value of strlen function
int length = strlen(string);
printf("Length of string is: %d",length);
return 0;
}
1. Take a string as input and store it in the array string.
2. Call the strlen() function and store its value in the length variable.
3. Print the Length of the string.
Time Complexity: O(n)
Time complexity of the standard strlen function is O(n), where n is length of string.
Space Complexity: O(n)
Space is required to store a sting, so the space complexity is O(n).
In this case, we enter the string “Sanfoundry Programs” as input to find the length of the string.
Enter the string: Sanfoundry Programs Length of string is: 19
In this approach, we will find the length of the string using recursion.
Example:
String: “Hello”
Len(“Hello”) = return 1 + Len(“ello”)--------------------------------------------- 5 return 1 + Len(“llo”)----------------------------------- 4 return 1 + Len(“lo”)------------------------- 3 return 1 + Len(“o”)--------------- 2 return 1 -------------- 1
Here is source code of the C program to find the length of a string using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the length of a string using recursion
*/
#include <stdio.h>
#include <string.h>
int length(char str[],int i)
{
//Base case
if(str[i]=='\0')
return 0;
//Recursive calls
return 1 + length(str,i+1);
}
int main ()
{
char string[30];
// input the string
printf ("Enter the string\n");
gets (string);
// Calling length function
int len = length(string,0);
printf("Length of string is: %d",len);
return 0;
}
1. Take a string as input and store it in the array string.
2. Declare function to find length which recursively calls itself and return (1 +Length of rest string) with a base case to stop when a backspace character is encountered.
3. Print value returned by function as the length of string.
Time Complexity: O(n)
As there are n function calls, the time complexity is O(n), where n is the length of the string.
Space Complexity: O(n)
Space is required to store a sting, so the space complexity is O(n).
In this case, we enter the string “Hello” as input to find the length of the string.
Enter the string: Hello Length of string is: 5
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Apply for C Internship
- Watch Advanced C Programming Videos
- Buy Computer Science Books