ADVERTISEMENT
ADVERTISEMENT

C program to calculate length of the string using recursion

In this program, we will read a string through user and calculate the length of the string using recursion in c programming language. We will not use standard function strlen() to calculate the length of the string.

Length of the string program using recursion

/*function to calculate length of the string using recursion.*/
 
#include <stdio.h>
 
//function to calculate length of the string using recursion
int stringLength(char *str)
{
    static int length=0;
    if(*str!=NULL)
    {
        length++;
        stringLength(++str);
    }
    else
    {
        return length;
    }
}
int main()
{
    char str[100];
    int length=0;
     
    printf("Enter a string: ");
    gets(str);
     
    length=stringLength(str);
     
    printf("Total number of characters (string length) are: %d\n",length);
 
    return 0;
}

Output

Enter a string: www.includehelp.com
Total number of characters (string length) are: 19

C Recursion Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.