相关的判断字符的函数
isalnum():判断字符是否是字母或数字isalpha():判断字符是否是字母isdigit():判断字符是否是数字ispunct():判断字符是否是标点符号isupper():判断字符是否是大写字母islower():判断字符是否是小写字母toupper():转换为大写字母tolower():转换为小写字母isxdigit():判断字符是否是十六进制数
思路: 1、回文串判断,首尾比较。忽略大小写,所以可以将字符转换为小写比较 2、只考虑数字和字母,可以使用isalnum(数字或字母返回非0,其他返回0)
bool isPalindrome(string s
) {
int i
= 0;
int j
= s
.size() - 1;
while (i
< j
) {
while (i
< j
&& !isalnum(s
[i
])) {
i
++;
}
while (i
< j
&& !isalnum(s
[j
])) {
j
--;
}
if(i
< j
&& tolower(s
[i
]) != tolower(s
[j
])) {
return false;
}
i
++;
j
--;
}
return true;
}
关于字符串的函数
int atoi(char *str) 将字符串转换成一个整数值,成功返回转换后的数值,失败则返回0。
#include<iostream>
#include<stdlib.h>
using namespace std
;
int main(){
string str
= "-10";
cout
<<atoi(str
.c_str())<<endl
;
return 0;
}
itoa()函数的原型:char* itoa(int value, char *str,int radix),将指定整数值value转换为radix进制的整数,存放在str所指的空间,并返回str所指的地址。
#include<stdio.h>
#include<stdlib.h>
int main(){
char* str
= new char(10);
char* s
= itoa(-100, str
, 10);
printf("%s\n", str
);
printf("%s\n", s
);
printf("%d\n", s
);
printf("%d\n", str
);
return 0;
}