C语言 实现句子反转: I like Beijing. -> Beijing. like I ;I love you. -> you. love I

C语言 实现句子反转: I like Beijing. -> Beijing. like I ;I love you. -> you. love I

//语句反转: I love you. -> you. love I
void func(const char *str)
{
    
    
	int i = 0, j = 0, n = 0, m = 0;
	char temp[256] = {
    
    0};
	int len = strlen(str);
	
	for (i = len - 1; i >= 0; i--)
	{
    
    
		if (' ' != str[i])
		{
    
    
			n++;
		}
		if ((' ' == str[i]) || (0 == i))
		{
    
    
			if (0 == i)
			{
    
    
				temp[m] = str[i];
				m++;
			}
			for (j = 0; j < n; j++)
			{
    
    
				temp[m] = str[i + 1 + j];
				m++;
			}
			if (0 != i)
			{
    
    
				temp[m] = str[i];
				m++;
			}
			n = 0;
		}
	}
	printf("%s\n",temp);
	return;
}

验证:
pls input chars:I love you.
you. love I
pls input chars:I like Beijing.
Beijing. like I
pls input chars:my name is ZHONGGUO!!!
ZHONGGUO!!! is name my

猜你喜欢

转载自blog.csdn.net/qq_19693355/article/details/127760441
今日推荐