反转字符串中的单词

时间:2014-04-09 20:58:07

标签: c

我正在尝试编写一个只反转字符串中单词顺序的函数。关于如何连接字符的任何提示?有我的代码。

 #include <stdio.h>
 #include <string.h>
 #include "header.h"


/* Reverse the string character by character*/
void reverseWord (const char *str, char *wordreverse)
{
    char *ptr;
    int i, n = 0, length;


    length = strlen( str);
    strcpy( wordreverse, str);


    ptr = strtok( wordreverse, " ");



    while( ptr!= NULL) 
    {
            ptr = strtok(NULL, " "); /*get next token*/

    }

   printf("\nTokens in reverse order are:\n");

    for(i = length-1; i>=0; i--)
    {

            printf("%s\n",  &wordreverse[i]);
            ptr = strcat( &wordreverse[i], " ");

    }
        return 0;
 }

0 个答案:

没有答案