« Length of String using recursion

Problem

Find length of a string using recursion

Input Format :

String S

Output Format :

Integer representing length of string

Constraints :

0 <= |S| <= 1000

where |S| represents length of string S.

Sample input 1:

abcde

Sample output1:

5

Sample input 2:

ILoveIndia

Sample output 2:

10

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14int length(char str[])
15{
16 if (str[0] == '\0')
17 {
18 return 0;
19 }
20 return 1 + length(str + 1);
21}
22
23int main()
24{
25 char str[100];
26 cin >> str;
27 int len = length(str);
28 cout << len << endl;
29 return 0;
30}