Here you will get C++ program to find length of a string without using library function.
We iterate through the string until null character ‘\0’ is encountered and increase a counter variable for each iteration. Finally print the counter variable, it contains the length of string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<iostream> #include<stdio.h> using namespace std; int main() { char a[30]; int i; cout<<"Enter a string:"; gets(a); for(i=0;a[i]!='\0';++i); cout<<"\nLength of the string '"<<a<<"' is "<<i; return 0; } |
Output
Enter a string:hello hi
Length of the string ‘hello hi’ is 8
hey, i get a error that “gets should have a prototype”.
I too have the same problem.
can we use strlen instead of for loop???
I guess you did not read the title of the blog. It clearly says “Without using Library functions”. The function “strlen()” is library function.
Do we need to use string header file in this program
Yes you must……
Thank you guys plz make some more programs it helos us so much