反转句子中的单词

题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。
例如输入“I am a student.”,则输出“student. a am I”。

这种题是很无聊的,而且用istream什么就没有可以做的了,思路:倒着遍历字符串,查找到一个空格后,输出本空格与上次空格之间的内容。可以用\0临时替代printf。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1024

void reverse_print(char* str)
{
	// Get len
	int len = strlen(str);
	int i = len-1;
	int last = len;
	char tmp;
	// Find from last to first	
	while(i>=0)
	{
		if( str[i]==' ' || i==0)
		{
			tmp = str[last];
			str[last] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1024

void reverse_print(char* str)
{
	// Get len
	int len = strlen(str);
	int i = len-1;
	int last = len;
	char tmp;
	// Find from last to first	
	while(i>=0)
	{
		if( str[i]==' ' || i==0)
		{
			tmp = str[last];
			str[last] = '\0';
			if(i==0 && str[i]!=' ')
			{
				printf("%s ", str+i);
			}
			else
			{
				printf("%s ", str+i+1);
			}
			str[last] = tmp;
			last = i;
		}
		i--;
	}
}

int main()
{
	char* buf = NULL;
	int len = 0;
	len = getline(&buf, &len, stdin);
	if(len==-1)
	{
		printf("getline error.\n");
		return -1;	
	}else
	{
		buf[len] = '\0';
		buf[len-1] = '\0'; // \n
		reverse_print(buf);
		if(buf)
		{
			free(buf);
		}
	}
	return 0;
}
'; if(i==0 && str[i]!=' ') { printf("%s ", str+i); } else { printf("%s ", str+i+1); } str[last] = tmp; last = i; } i--; } } int main() { char* buf = NULL; int len = 0; len = getline(&buf, &len, stdin); if(len==-1) { printf("getline error.\n"); return -1; }else { buf[len] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1024

void reverse_print(char* str)
{
	// Get len
	int len = strlen(str);
	int i = len-1;
	int last = len;
	char tmp;
	// Find from last to first	
	while(i>=0)
	{
		if( str[i]==' ' || i==0)
		{
			tmp = str[last];
			str[last] = '\0';
			if(i==0 && str[i]!=' ')
			{
				printf("%s ", str+i);
			}
			else
			{
				printf("%s ", str+i+1);
			}
			str[last] = tmp;
			last = i;
		}
		i--;
	}
}

int main()
{
	char* buf = NULL;
	int len = 0;
	len = getline(&buf, &len, stdin);
	if(len==-1)
	{
		printf("getline error.\n");
		return -1;	
	}else
	{
		buf[len] = '\0';
		buf[len-1] = '\0'; // \n
		reverse_print(buf);
		if(buf)
		{
			free(buf);
		}
	}
	return 0;
}
'; buf[len-1] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1024

void reverse_print(char* str)
{
	// Get len
	int len = strlen(str);
	int i = len-1;
	int last = len;
	char tmp;
	// Find from last to first	
	while(i>=0)
	{
		if( str[i]==' ' || i==0)
		{
			tmp = str[last];
			str[last] = '\0';
			if(i==0 && str[i]!=' ')
			{
				printf("%s ", str+i);
			}
			else
			{
				printf("%s ", str+i+1);
			}
			str[last] = tmp;
			last = i;
		}
		i--;
	}
}

int main()
{
	char* buf = NULL;
	int len = 0;
	len = getline(&buf, &len, stdin);
	if(len==-1)
	{
		printf("getline error.\n");
		return -1;	
	}else
	{
		buf[len] = '\0';
		buf[len-1] = '\0'; // \n
		reverse_print(buf);
		if(buf)
		{
			free(buf);
		}
	}
	return 0;
}
'; // \n reverse_print(buf); if(buf) { free(buf); } } return 0; }

2 thoughts on “反转句子中的单词

    1. coder4 Post author

      我也知道Python各种好用啊。。这不是纯粹为了无聊的面试笔试么。。

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *