【leetcode】151. 翻转字符串里的单词

char * reverseWords(char * s){
    char* stack[100];
    int pst = 0, len=strlen(s);
    char* retStr = (char*)calloc(len+1, sizeof(char));
    char* temp;
    temp = strtok(s, " ");
    while (temp)
    {
        stack[pst++] = temp;
        temp = strtok(NULL, " ");
    }
    for (int i = pst-1; i >= 0 ; i--)
    {
        strcat(retStr, stack[i]);
        strcat(retStr, " ");
    }
    retStr[strlen(retStr) - 1] = '';
    return retStr;
}
char * reverseWords(char * s){
    char* stack[100];
    int pst = 0, len=strlen(s);
    char* retStr = (char*)calloc(len+1, sizeof(char));
    char* temp;
    temp = strtok(s, " ");
    while (temp)
    {
        stack[pst++] = temp;
        temp = strtok(NULL, " ");
    }
    for (int i = pst-1; i >= 0 ; i--)
    {
        strcat(retStr, stack[i]);
        strcat(retStr, " ");
    }
    retStr[strlen(retStr) - 1] = '';
    return retStr;
}
原文地址:https://www.cnblogs.com/ganxiang/p/14182099.html