how to print a string using recursion in c

Solutions on MaxInterview for how to print a string using recursion in c by the best coders in the world

showing results for - "how to print a string using recursion in c"
Marie
15 Feb 2019
1#include <stdio.h>
2
3/**
4* rec - a function that prints a string
5* Desc: use recursion to print a string one letter at a time
6*/
7void rec(char *s)
8{
9        if (*s == '\0')
10                return;
11        printf("%c", *s);
12        rec(s + 1);
13}
14
15/**
16* main - start of this program
17* Desc: implements on rec()
18* Return 0 on success
19*/
20int main()
21{
22        char str[100] = "My test string";
23        rec(str);
24        return 0;
25}
Erika
02 Jul 2016
1#include <stdio.h>
2
3/**
4 * recursion - prints a string followed gy a new line
5 * @s: pointer to the location of the string characters
6 */
7void recursion(char *s)
8{
9  		if (*s(
10        {
11          		putchar(*s);
12                recursion(s + 1);
13        }
14        else
15        {
16        		putchar('\n');
17        }
18}
19int main(void)
20{
21  		recursion("Print this string");
22  		return 0;
23}