Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

17
Feb

String Reversal

Related Blog Items

/*!
 * string_reverse.c: Reverse a string  Feb 17, 2006
 */
#include <stdio.h>
#include <string.h>

void reverse_str(char *str, int start, int end)
    {
    char c = str[start];
    str[start] = str[end];
    str[end] = c;

    start++;
    end–;
    if (start < end)
        reverse_str(str, start, end);
    }

int main ()
    {
    char str[] = "vijoeyz";
    int len = strlen(str);
    reverse_str(str, 0, len-1);
    puts(str);
    return 0;
    }

Popularity: 23%

You need to log on to convert this article into PDF


Related Blog Items

2 Comments

  • ponnada  said:

    Here is one more algo for string reversal using recursive function… this program is slightly different than Vijoeyz’s version but similar and does the same thing….

    int string_reverse(const char * src, char *dst)
    {
    int len;

    if (*src == ”)
    {
    return 0;
    }

    len = string_reverse(src + 1, dst);
    *(dst + len) = *src;

    return len + 1;
    }


  • ponnada  said:

    I’ve written the above program (which is in comment section) as a part of palindrome checker for one of my client in a freelancing site..


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image