1. 程式人生 > >字符數組為:"i am a student",將數組改為"student a am i

字符數組為:"i am a student",將數組改為"student a am i

void main 字符 count c語言 ring stdio.h tdi print

有一個字符數組的內容為:"i am a student",
請你將數組的內容改為"student a am i".
要求:
不能使用庫函數。
只能開辟有限個空間(空間個數和字符串的長度無關)。

#include<stdio.h>
#include<string.h>
int MyStrlen(char *str)
{
    int count = 0;
    while (*str != ‘\0‘)
    {
        str++;
        count++;
    }
    return count;
}
void Reverse(char *left, char *right)
{
    char temp;
    while (left < right)
    {
        temp = *left;
        *left = *right;
        *right = temp;
        left++;
        right--;
    }
}
void ReverseSentence(char *str)
{
    char *left = str;
    char *right = str + MyStrlen(str) - 1;
    char *p=str;
    Reverse(left,right);
    while (*p != ‘\0‘)
    {
        char *b = p;
        while (*p != ‘ ‘&&*p != ‘\0‘)
        {
            p++;
        }
        Reverse(b, p-1);
        if (*p==‘ ‘)
        {
            p++;
        }
    }
}
int main()
{
    char str[] = "i am a student";
    ReverseSentence(str);
    printf("%s\n",str);
    system("pause");
    return 0;
}           

c語言的靈魂》》》指針

字符數組為:"i am a student",將數組改為"student a am i