C函数:用指针的方法将字符串逆序

#include"stdio.h"

#include

#include"string.h"

typedef struct StrLink {

char cValue;

struct StrLink *next;

}STRNODE;

void RevertStringFunc( STRNODE **OrginalString )

{

STRNODE *pHead = *OrginalString;

STRNODE *pTail = NULL;

STRNODE *pTemp = pHead;

pTemp = pHead->next;

pHead->next = NULL;

while (pTemp!=NULL)

{

pTail = pTemp;

pTemp = pTemp->next;

pTail->next = pHead;

pHead = pTail;

}

*OrginalString = pHead;

return;

}

char* MainFuc(char *str)

{

STRNODE *GenerateString; /* 保存临时的字符串,用链表管理。 */

STRNODE *InsertNode,*p;

char *cpOutputString = NULL;

char *pc = NULL;

GenerateString = (STRNODE*)malloc(sizeof(STRNODE));

cpOutputString = malloc(100*sizeof(char));

pc = cpOutputString;

p = GenerateString;

GenerateString->cValue = *str;

str++;

/* 将输入的字符串拷贝到临时结构体中,用于转化用 */

while (*str != '/0')

{

InsertNode = (STRNODE*)malloc(sizeof(STRNODE));

InsertNode->cValue = *str;

InsertNode->next = NULL;

GenerateString->next = InsertNode;

GenerateString = GenerateString->next;

str++;

}

GenerateString->next = NULL;

RevertStringFunc(&p);

while (p!=NULL)

{

*cpOutputString = p->cValue;

p=p->next;

cpOutputString++;

}

*cpOutputString = '/0';

/* 将转换后的结果拷贝到输出字符串中 */

return pc;

}

void main()

{

char *str = "ABCDEFG";

char *output;

printf("Original String is: %s/n",str);

output = MainFuc(str);

printf("After Revert is: %s /n",output);

}

你可能感兴趣的:(菜鸟文章)