C Program to Find Length of String

  • Write a C program to find length of a string.

A string in c is a one dimensional character array terminated by null character '\0'. So, a null-terminated string contains the characters that comprise the string followed by a null terminating character('\0').


char string[6] = "HELLO";
string[0] = 'H'
string[1] = 'E'
string[2] = 'L'
string[3] = 'L'
string[4] = 'O'
string[5] = '\0'
Length of string = 5
While calculating length of string we don't count null character.

C Program to find length of string using strlen function

We first take a string as input from user using gets function. Then we call strlen library function(defined inside string.h) by passing input string. strlen function returns number of characters in the string, including space characters and excluding null character.


size_t strlen( const char * str );

/*
* C Program to print length of a string
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main(){
    char inputString[100], stringLength;
    printf("Enter a string for length calculation \n");
    gets(inputString);
    /* strlen calculates the length of string 
       without including the null character ('\0'). */
    stringLength = strlen(inputString);
    printf("Length of %s is %d \n", inputString, stringLength);
    
    getch();
    return 0;
}
Program Output
Enter a string for length calculation
TechCrashCourse
Length of TechCrashCourse is 15
Enter a string for length calculation
Tech Crash Course
Length of Tech Crash Course is 17

C program to find length of string using loop

We first take a string as input from user using gets function. We iterate starting from first character inputString[0] till we don't find a null character. Here we are using for loop, but similarly we can use while or do-while loop also.


/*
* C Program to calculate length of a string using loop
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main(){
    char string[100], stringLength;
    printf("Enter a string for length calculation \n");
    gets(string);
    /* Starting from first character, keep on 
    incrementing counter till you reach '\0' */
    for(stringLength=0; string[stringLength]!='\0'; stringLength++);
    
    printf("Length of %s is %d \n", string, stringLength);
    
    getch();
    return 0;
}
Program Output
Enter a string for length calculation
String Length
Length of String Length is 13

C program to find length of string using user defined function

This program uses a user defined function 'getStringlength' to find the length of string. Function getStringlength starts traversing the string from index 0 till it reaches null terminating character and keeps on counting characters in variable 'length'.

/*
* C Program to calculate length of a string using function
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

int getStringlength(char *inputString);
int main(){
    char inputString[100], stringLength;
    printf("Enter a string for length calculation \n");
    gets(inputString);
    
    stringLength = getStringlength(inputString);
    
    printf("Length of %s is %d \n", inputString, stringLength);
    
    getch();
    return 0;
}

/*
 *  Function to calculate length of a string
 */
int getStringlength(char *inputString){
    int length = 0;
    while(inputString[length] != '\0'){
        length++;
    }
    return length;
}
Program Output
Enter a string for length calculation
CProgramming
Length of CProgramming is 12

Related Topics
C program to concatenate strings
C program to compare two strings
C program to reverse a string
C program to check string is palindrome
C program to remove vowels from a string
C program to remove extra spaces from string
C program to sort characters of a string
List of all C programs