she is a girl 倒置

前端之家收集整理的这篇文章主要介绍了she is a girl 倒置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//面试碰到的一道题,"she is a girl"字符串倒置成"girl a is she",给我一张纸就让我写程序,然后就成功的被pass掉了。程序真的不是在纸上写出来的。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
	char str[] = "she is a girl";
	char *tmp = (char*)malloc(strlen(str) + 1);
	memset(tmp,strlen(str)+1);
	int count = 0;
	char * p = str;
	while(strsep(&p," "))
		count++;

	while(count--){
		p = str;
		for(int i = 0;i<count;i++)
		    p+=(strlen(p)+1);

		if(count == 0)
		    strcat(tmp,p);
		else
		    strcat(strcat(tmp,p)," ");
	}
	puts(tmp);//"girl a is she"
	free(tmp);
	return 0;
}
原文链接:https://www.f2er.com/javaschema/286226.html

猜你在找的设计模式相关文章