Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to reverse a string recursively in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

char *reverse_string_recursively(char s[], int i, int len) {
    if (i >= len / 2) {
        return s; 
    }

    char tmp = s[i];
    s[i] = s[len - i - 1]; 
    s[len - i - 1] = tmp;

    reverse_string_recursively(s, i + 1, len);
}

int main()
{
    char str[] = "c c++ java";
    
    reverse_string_recursively(str, 0, strlen(str));
    
    printf("%s", str);

    return 0;
}



/*
run:

avaj ++c c

*/

 



answered Jul 25, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

void reverse_string_recursively(char *str, int start, int end) {
    if (start >= end) {
        return; 
    }

    // Swap characters at start and end
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;

    // Recursive call for the next pair
    reverse_string_recursively(str, start + 1, end - 1);
}

int main() {
    char str[] = "c c++ java";
    int length = strlen(str);

    reverse_string_recursively(str, 0, length - 1);

    printf("%s\n", str);

    return 0;
}



/*
run:

avaj ++c c

*/

 



answered Jul 19, 2025 by avibootz
...