首页 > 代码库 > 字符串转置

字符串转置

/*************************************************************************    > File Name: revert.c    > Created Time: 2014年06月07日 星期六 22时39分21秒 ************************************************************************/#include<stdio.h>#include<string.h>//基于字符数组的转置void revert(char *str){    printf("method reverse\n");    int length = strlen(str);    char tem;    int i;    for(i = 0; i < length/2; i++)    {        tem =  str[i];        str[i] =  str[length-i-1];        str[length-i-1] = tem;    }}//基于c指针的转置void revert1(char *str){    printf("method revers1:\n");    int length = strlen(str);    char tem;    int i;    for(i = 0; i<length/2; i++)    {        tem = *(str+i);        *(str+i) =  *(str+length-i-1);        *(str+length-i-1) = tem;    }}//纯c语言实现,不含任何库函数void revert2(char *str){    printf("mehtod revert2\n");    int len = 0;    while(*(str+len) != \0)len++;    char *start = str;    char *end = str+len-1;    int size = len/2,i=0;    while(++i<=size)    {        char tem = *end;        *end = *start;        *start = tem;        start++;        end--;    }    //printf("%s\n",str);}int main(){    char str[100];    printf("请输入一个字符串:\n");    scanf("%s",str);    revert(str);    printf("字符串转置后:\n");    printf("%s\n",str);    revert1(str);    printf("字符串转置后:\n");    printf("%s\n",str);    revert2(str);    printf("字符串转置后:\n");    printf("%s\n",str);    return 0;}