浪潮面试题(将一个句子中的单词反转)例:i am bob 输出 bob am i

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
/*
strtok:
char *strtok( char *str1, const char *str2 );  *****************str1一定是要分配内存的数据******************


功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。

例如:

char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;

result = strtok( str, delims );

while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
以上代码的运行结果是:

result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"

*/
//思路1:先申请一片内存保存字符串,用strtok函数拆分字符串,定义一个指针数组,指向拆分后的每一段,然后申请内存,用strcat函数将指针数组中的字符串拼接
char *change(const char *str)
{
	if (str == NULL)
	{
		return NULL;
	}
	int len = strlen(str);
	char *copy_str = (char *)malloc(sizeof(char)*(len + 1));
	if (copy_str == NULL)
	{
		return NULL;
	}
	memset(copy_str, 0, (len + 1));
	strcpy(copy_str, str);
	char *pstr = NULL;
	pstr = strtok(copy_str, " ");
	if (pstr == NULL)
	{
		return NULL;
	}
	char *p[1024];
	memset(p, 0, sizeof(p));
	int count = 0;
	p[count++] = pstr;
	while (1)
	{
		pstr = strtok(NULL, " ");
		if (pstr == NULL)
		{
			break;
		}

		p[count++] = pstr;
	}

	char *return_str = (char *)malloc(sizeof(char)*(len + 1));
	if (return_str == NULL)
	{
		return NULL;
	}
	memset(return_str, 0, len + 1);
	for (int i = count - 1; i >= 0; i--)
	{
		strcat(return_str, p[i]);
		if (i != 0)
		{
			strcat(return_str, " ");
		}
	}
	if (copy_str != NULL)
	{
		free(copy_str);
	}
	return return_str;

}
void test()
{
	char *str = "I                                                  am   a student";
	char * my_str = change(str);
	if (my_str == NULL)
	{
		return;
	}
	printf("%s", my_str);
	if (my_str != NULL)
	{
		free(my_str);
	}

}
//思路2:自己写个类似strtok的函数,先保存首地址,然后判断每一次遇到的第一个空格改成\0,然后判断仍然是空格的话跳过,把函数指针指到下一个单词的头部,如此循环
char *change_two(const char *str)
{
	if (str == NULL)
	{
		return NULL;
	}
	int len = strlen(str);
	char *copy_str = (char *)malloc(sizeof(char)*(len + 1));
	if (copy_str == NULL)
	{
		return NULL;
	}
	memset(copy_str, 0, (len + 1));
	strcpy(copy_str, str);
	char *temp=copy_str;
	char *p[1024] = { 0 };
	while (*copy_str == ' ')
	{
		copy_str++;
	}
	p[0] = copy_str;
	int count = 1;
	while (*copy_str != '\0')
	{
		if (*copy_str != ' ')
		{
			copy_str++;
		}
		else
		{
			*copy_str++ = '\0';
			while (*copy_str == ' ')
			{
				copy_str++;
			}
			if (*copy_str == '\0')
			{
				break;
			}
			p[count++] = copy_str;
		}
	}
	char *return_str = (char *)malloc(sizeof(char)*(len + 1));
	if (return_str == NULL)
	{
		return NULL;
	}
	memset(return_str, 0, len + 1);
	for (int i = count - 1; i >= 0; i--)
	{
		strcat(return_str, p[i]);
		if (i != 0)
		{
			strcat(return_str, " ");
		}
	}
	if (temp != NULL)
	{
		free(temp);
	}
	return return_str;

	
}
void test_two()
{
	char *str = "  I am a student   ";
	
	char * my_str = change_two(str);
	if (my_str == NULL)
	{
		return;
	}
	printf("%s", my_str);
	if (my_str != NULL)
	{
		free(my_str);
	}

}
void main()
{
	test_two();
	system("pause");
}

你可能感兴趣的:(C语言)