递归应用-字符串反转

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void reverse_string(char *x, int start, int end)
{
    if (x == NULL)
        return;
    char ch;
    if (start >= end)
       return;

    ch = *(x+start);
    *(x+start) = *(x+end);
    *(x+end) = ch;

    //Function calling itself: Recursion
    reverse_string(x, ++start, --end);
}


void main()
{
    //This array would hold the string up to 64 char
    char string_array[64];
    printf("Enter any string:");
    scanf("%s", string_array);

    //Calling our user defined function
    reverse_string(string_array, 0, strlen(string_array)-1);
    printf("\nReversed String is: %s",string_array);

    return 0;
}



上一篇:洛谷P4735 最大异或和


下一篇:单词首字母大写PTA