1.内容简介
(1)字符串常见问题;
(2)问题的解法及思维;
(3)涉及的知识点。
2.问题:
(1)请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
代码:
/*功能说明:实现字符串中' ' 替换位 "%20"
参数说明:
char *str; 目标字符串的首地址;
int length; 目标字符串的长度;
*/
void replaceSpace(char *str, int length)
{
if (str == NULL || length < 0)
return; //如果字符串位空则退出,避免指针访问越界
int count = 0;
for (int i = 0; i < length; i++)
{
if (str[i] == ' ')
count++; //计算字符串中空格的数量 目标字符串的长度为 length+2*count
}
for (int i = length - 1; i >= 0; i--)//逆序遍历
{
if (str[i] != ' ')
{
str[i + 2 * count] = str[i];
}
else
{
count--;
str[i + 2 * count] = '%';
str[i + 2 * count + 1] = '2';
str[i + 2 * count + 2] = '0';
}
}
}
(2)字符串的正则表达式:
请实现一个函数用来匹配包括 ‘.’ 和 ‘’ 的正则表达式。模式中的字符 ‘.’ 表示任意一个字符,而 '’ 表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但是与"aa.a"和"ab*a"均不匹配。
/*功能说明:字符串正则表达式
参数说明:
char *str; 原字符串;
char* pattern; 匹配字符串;
*/
bool match(char* str, char* pattern)
{
if (*str == '\0' && *pattern == '\0')//都为空 匹配成功
return true;
if (*str != '\0' && *pattern == '\0')//字符串不为空 ,匹配串为空
return false;
//if the next character in pattern is not '*'
if (*(pattern + 1) != '*')
{
if (*str == *pattern || (*str != '\0' && *pattern == '.'))//值相等,或者值不等但匹配字符是'.'通配时,指针同时后移 (1)
return match(str + 1, pattern + 1);
else
return false;//不满足(1) 且下一个匹配字符不是'*' 时返回错误
}
//if the next character is '*'
else
{
if (*str == *pattern || (*str != '\0' && *pattern == '.')) //值相等,或者值不等但匹配字符是'.'通配时
return match(str, pattern + 2) || match(str + 1, pattern);
else
return match(str, pattern + 2);//值不相等,匹配字符不是是'.'通配时 可以把匹配字符指针后移两位
}
}
(3)表示数值的字符串
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。
/*功能说明:字符串是否是数值
参数说明:
char *string; 原字符串;
*/
bool isNumeric(char* string)
{
std::string stemp = string;
int iLength = stemp.length();
if (!iLength)//字符串为空 返回false
return false;
int i = 0;
if (stemp[0] == '+' || stemp[0] == '-')//第一个字符可以是正负号,如果是正负号则后移一位
i++;
char temp;
int ipositiond = 0;
int ipositione = 0;
for (; i < iLength; i++)
{
temp = stemp[i];
if (isdigit(temp))//是否为数字
continue;
else if (temp == 'e' || temp == 'E')//如果是E
{
if (ipositione)//是否出现过e
return false;
char ctemp = stemp[i + 1];
ipositione = i;
if (ctemp == '-' || ctemp == '+')//e后面是不是符号位
i++;
if (ipositione < ipositiond || ipositione == iLength - 1)//e位置规则,后面不能有'.',也不能是最后一位
return false;
}
else if (temp == '.')//如果是浮点符号
{
if (ipositiond)//出现浮点符号之前是否出现过浮点符号
return false;
ipositiond = i;
if (ipositione && ipositione < ipositiond)//'.'位置规则,'.'不能在e后面
return false;
}
else
return false;
}
return true;
}
(4)字符流中第一个不重复的字符
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
如果当前字符流没有存在出现一次的字符,返回#字符。
/*功能说明:字符串流中第一个只出现一次的字符
参数说明:
char *string; 原字符串;
*/
char FirstAppearingOnce(char *string)
{
std::string stemp = string;
int iLength = stemp.length();
if (!iLength)//字符串为空 返回false
return '#';
int chSet[128]{ 0 };
char ctemp;
for (int i = 0; i < iLength; i++)
{
ctemp = string[i];
chSet[ctemp]++;
}
for (int i = 0; i < iLength; i++)
{
ctemp = string[i];
if (chSet[ctemp] == 1)
return ctemp;
}
return '#';
}