字符串拆分

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
	一道面试题,"i love china",倒序输出字符串,单词不倒序.
	一开始搞错了思路,写应用多了就总往应用上靠.其实用指针遍历一遍就好.
*/
void outputWord(const char* str,int nlen);
int main(void)
{
	const char* str = "i love china";
	unsigned int nlen = strlen(str);
	char* ptr = (char*)(str + nlen-1);
	int nLen4Word = 0;
	while (ptr != str-1)
	{
		nLen4Word++;		
		if (*ptr == ' ')
		{
			outputWord(ptr+1,nLen4Word-1);
			printf(" ");
			nLen4Word = 0;
		}
		ptr--;
	}
	if (nLen4Word > 0)
	{
		outputWord(str,nLen4Word);
	}
	printf("
");
	return 0;
}
void outputWord(const char* str,int nlen)
{
	//printf("str is %s,len is %d
",str,nlen);
	for (int i = 0;i < nlen; i++)
	{
		printf("%c",str[i]);
	}
	
}
原文地址:https://www.cnblogs.com/decwang/p/4710593.html