题目:将字符数组a转换成apple an is this,可以把它叫做字符数组单词逆序
要求:1、不能开辟新的数组或者其他内存单元,使其空间复杂度为O(1)
{空间复杂度:空间复杂度是对一个算法在运行过程中临时占用存储空间大小的量度
如当一个算法的空间复杂度为一个常量,即不随被处理数据量n的大小而改变时,可表示为O(1)
表示该程序所占用的空间和所用数据量无关}
要求:1、不能开辟新的数组或者其他内存单元,使其空间复杂度为O(1)
{空间复杂度:空间复杂度是对一个算法在运行过程中临时占用存储空间大小的量度
如当一个算法的空间复杂度为一个常量,即不随被处理数据量n的大小而改变时,可表示为O(1)
表示该程序所占用的空间和所用数据量无关}
2、不能使用C++特性
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "this is an apple";
int str_len = sizeof(str);
cout << "Length of string is " << str_len << endl;
// 翻转字符串
for (int i = 0; i < str_len/2 ; i++)
{
str[str_len - 1] = str[i];
str[i] = str[str_len -2 -i];
str[str_len - 2 - i] = str[str_len - 1];
}
str[str_len - 1] = '\0';
cout << str << endl;
//纠正字母
int word_len = 0;
for (int i = 0; i < str_len; i++)
{
word_len++;
if (str[i] == '\0' || str[i] == ' ')
{
for (int j = 0; j < word_len / 2; j++)
{
str[i] = str[i - word_len + 1 + j];
str[i - word_len + 1 + j] = str[i - 1 - j];
str[i - 1 - j] = str[i];
}
str[i] = ' ';
word_len = 0;
}
}
str[str_len - 1] = '\0';
cout << str << endl;
return 0;
}