#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
void turn(char* res, char* des, int n)
{
if (res == NULL)
return ;
int i, j;
const int resSize = strlen(res);
for (i = 0, j = 0; i < resSize; i++, j++)
{
if (res[i] != ' ')
{
des[j] = res[i];
}
else
{
des[j++] = '%';
des[j++] = '2';
des[j] = '0';
}
}
des[j] = '\0';
puts(des);
}
bool turnDirectly(char* str, int spaceSize)
{
if (str == NULL || spaceSize <= 0)
return false;
int i, j, number = 0;
const int realSize = strlen(str);
for (i = 0; i < realSize; i++)
{
if (str[i] == ' ')
{
number ++;
}
}
if (realSize + number * 2 > spaceSize)
return false;
else
{
for (i = realSize + number * 2, j = realSize; i > j && i >= 0; i--, j--)//注意此处i > j的判断可以使得在已经确定剩下的字符串没有空格的情况下提前结束搜索和改变
{
if (str[j] != ' ')
str[i] = str[j];
else
{
str[i--] = '0';
str[i--] = '2';
str[i] = '%';
}
}
}
puts(str);
return true;
}
int main()
{
char str[40];
gets(str);
int i;
const int strSize = strlen(str);
int m = 0;
for (i = 0; i < strSize; i++)
{
if (str[i] == ' ')
m += 2;
}
char *des = new char[m + strSize];
turnDirectly(str, 40);
delete(des);
return 0;
}
#include <string>
#include <string.h>
using namespace std;
void turn(char* res, char* des, int n)
{
if (res == NULL)
return ;
int i, j;
const int resSize = strlen(res);
for (i = 0, j = 0; i < resSize; i++, j++)
{
if (res[i] != ' ')
{
des[j] = res[i];
}
else
{
des[j++] = '%';
des[j++] = '2';
des[j] = '0';
}
}
des[j] = '\0';
puts(des);
}
bool turnDirectly(char* str, int spaceSize)
{
if (str == NULL || spaceSize <= 0)
return false;
int i, j, number = 0;
const int realSize = strlen(str);
for (i = 0; i < realSize; i++)
{
if (str[i] == ' ')
{
number ++;
}
}
if (realSize + number * 2 > spaceSize)
return false;
else
{
for (i = realSize + number * 2, j = realSize; i > j && i >= 0; i--, j--)//注意此处i > j的判断可以使得在已经确定剩下的字符串没有空格的情况下提前结束搜索和改变
{
if (str[j] != ' ')
str[i] = str[j];
else
{
str[i--] = '0';
str[i--] = '2';
str[i] = '%';
}
}
}
puts(str);
return true;
}
int main()
{
char str[40];
gets(str);
int i;
const int strSize = strlen(str);
int m = 0;
for (i = 0; i < strSize; i++)
{
if (str[i] == ' ')
m += 2;
}
char *des = new char[m + strSize];
turnDirectly(str, 40);
delete(des);
return 0;
}