练习题

来源:互联网 发布:网络渠道销售授权书 编辑:程序博客网 时间:2024/04/28 06:38

逆序句子但不改变单词的拼写

#include <stdio.h>#include <string.h>#define MAX_SIZE 1024void reverse_word(char *s2){       int i = 0;    int j = 0;    int low = 0;    char temp;    while(s2[i] != '\0')    {        if(s2[i] == ' ')        {            for(j = 0; j < (i - low) / 2; j++)            {                temp = *(s2 + low + j);                *(s2 + low + j) = *(s2 + i - 1 - j);                *(s2 + i - 1 - j) = temp;            }            low = i + 1;        }        i++;    }    for(j = 0; j < (i - low) / 2;j++)    {                temp = *(s2 + low + j);                *(s2 + low + j) = *(s2 + i - 1 - j);                *(s2 + i - 1 - j) = temp;    }}char * reverse_str(char *s1,int len){    int i;    char temp;    for(i = 0; i < len / 2; i++)    {        temp = *(s1 + i);        *(s1 + i) = *(s1 + len - i - 1);        *(s1 + len - i - 1) = temp;    }    reverse_word(s1);    return s1;}int main(){    char str[MAX_SIZE];    printf("please input a string:\n");    gets(str);    char * result = reverse_str(str,strlen(str));    printf("the result is %s",result);    return 0;}
0 0