
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to find length of a string without string.h and loop in C?
In this section we will see how to find length of a string without using string header file and loops in C. The string length finding problem can be solved without string.h very easily. We can use recursive function to do it.
But in this example we are not using recursion. We are using another trick to do that. We are using printf() function to get the length. The printf() function returns the number of character it has printed. If we print only that string using a printf() function, we can easily get the length of it.
Example Code
#include<stdio.h> main() { char* my_str = "This is a String"; printf("The string is: "); int length = printf("%s", my_str); printf("\nThe length of string is: %d", length); }
Output
The string is: This is a String The length of string is: 16
- Related Articles
- C++ Program to Find the Length of a String
- C program to find the length of a string?
- Java string length without using length() method.
- How to find the length of a string in TypeScript?
- 5 Different methods to find length of a string in C++?
- 5 Different methods to find the length of a string in C++?
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- Find longest length number in a string in C++
- Golang Program to find the length of a string
- Python Program to Calculate the Length of a String Without Using a Library Function
- Find length of longest subsequence of one string which is substring of another string in C++
- Find length of a string in python (3 ways)
- Find length of loop in linked list in C++
- Check if a string contains a palindromic sub-string of even length in C++
- How to calculate the length of the string using C#?

Advertisements