
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to find the length of a string?
The string is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
To find the length of a string we need to loop and count all words in the loop until the ‘\0’ character is matched.
For example
Input −naman
Output − string length is 5
Explanation − we need to iterate over each index of the string until reach the end of string means ‘\0’ which is the null character.
Example
#include <stdio.h> #include<string.h> int main() { char string1[]={"naman"}; int i=0, length; while(string1[i] !='\0') { i++; } length=i; printf(" string length is %d",length); return 0; }
Output
string length is 5
- Related Articles
- C++ Program to Find the Length of a String
- Golang Program to find the length of a string
- 5 Different methods to find the length of a string in C++?
- C++ program for length of a string using recursion
- C program to find the length of linked list
- PHP program to find the length of the last word in the string
- 5 Different methods to find length of a string in C++?
- C++ Program to find if the given string has Repeated Subsequence of Length 2 or More
- Program to find length of longest repeating substring in a string in Python
- How to find the length of a string in TypeScript?
- Write a program in Python to find the maximum length of a string in a given Series
- Program to find length of concatenated string of unique characters in Python?
- C++ Program to Find the Frequency of a Character in a String
- C# program to find the index of a word in a string
- C++ Program to Find the Number of Permutations of a Given String

Advertisements