字符串
1. 字符串反转
比如: how are you 转换成 you are how
#include <stdio.h>
void reverse(char * start, char *end){
if(NULL == start || NULL == end){
return;
}
char tmp;
while(start < end){
tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}
//字反转
char * reverse_word(char * src){
if(NULL == src){
return NULL;
}
char *start = src;
char *end = src;
while(*end != '\0'){
end++;
}
end--;
reverse(start, end);
end = start;
while(*start != '\0'){
if(*start == ' '){ //指向空字符
start++;
end++;
continue;
}else if(*end == ' ' || *end == '\0'){ //end指向空格或字符串结尾
reverse(start, --end);//注意这里先 -- 再传end
start = ++end;//注意这里先++,再赋值给start
}else{ //end指向字符
end++;
}
}
return src;
}
int main(){
char src[] = "how are you";
char *p = reverse_word(src);
printf("%s\n", p);
}
2. 判断是否为ip
#include <stdio.h>
int isValidValue(char *start, char *end){
int sum = 0;
while(start <= end){
if(*start < '0' || *start > '9'){
return -1;
}else{
sum = sum*10 + (*start - '0');
start++;
}
}
if(sum <0 || sum > 255){
return -1;
}
return 1;
}
int isValidFormat(char *src){
int num = 0;
int res = 0;
char *start = src;
char *end = src;
while(*start != '\0'){
printf("%s\n", start);
if(*start == '.'){
num++;
start++;
end++;
}else if(*end == '.' || *end == '\0'){
res = isValidValue(start, --end);
if(-1 == res){
return -1;
}else{
start = ++end;
}
}else{
end++;
}
}
if(3 != num){
return -1;
}
return 1;
}
int main(){
char *src = "24.1.2.4.1";
printf("%d\n", isValidFormat(src));
}
3. 字符串包含
一个长字符串a,一个短字符串b,如何判断b中的字符都在a中?
4. 字符串拼接
#include <stdio.h>
#define N 10
char * strconnect(char src[], char des[]){
if(NULL == src || NULL == des){
return NULL;
}
char *p = des;
while(*des != '\0'){
des ++;
}
while(*src != '\0'){
*des = *src;
des ++;
src ++;
}
*des = '\0';
return p;
}
int main(){
char src[N] = "abcd";
char des[N] = "1234";
strconnect(src, des);
printf("%s\n", des);
}
5. 字符串拷贝
#include <stdio.h>
char * strcopy(const char * src, char * des){
if(NULL == src || NULL == des){
return NULL;
}
char *p = des;
while(*src != '\0'){
*des = *src;
des++;
src++;
}
*des = '\0';
return p;
}
int main(){
const char *src = "abc";//src是指向 字符常量const char类型 的指针
char *des;
des = strcopy(src, des);//入参src与形参类型要一致,都是指向const char类型的指针
printf("%s\n", des);
}
6. 字符串移位包含
字符串s1,s2,判断字符串s2能否被s1循环移位得到的字符串包含。例如,给定s1=‘AABCD’,s2=’CDAA’,则s1向右移动3位变长CDAAB,包含s2
7. 查找文件中字符串总数和位置
8. 字符串转整形,整形转字符串
字符串转整形
#include <stdio.h>
int str2int(char *str){
if(NULL == str){
return -1;
}
int sign = 1;
int des = 0;
if('-' == *str){
sign = -1;
str++;
}else if('+' == *str){
str++;
}
while(*str != '\0'){
if(*str >= '0' && *str <= '9'){
des = des*10 + (*str - '0');
str++;
}else{
return -1;
}
}
return sign * des;
}
int main(){
char *str = "-1234";
int des = str2int(str);
printf("%d\n", des);
}
整形转字符串,方法一
#include <stdio.h>
#define N 100
char * int2str(int src, char des[]){
if(0 == src){
return NULL;
}
int i=0, j=0;
char tmp[N];
while(src > 0){
tmp[i] = src % 10 + '0';
src = src / 10;
i++;
}
i--;
while(i >= 0){
des[j] = tmp[i];
i--;
j++;
}
des[j] = '\0';
return des;
}
int main(){
int src = 1234;
char des[N];
int2str(src, des);
printf("%s\n", des);
}
整形转字符串,方法二
#include <stdio.h>
char * int2str(int src, char *des){
if(0 == src){
return NULL;
}
char * tmp = des;
while(src > 0){
*tmp = src % 10 + '0';
tmp++;
src = src / 10;
}
*tmp = '\0';
char strTmp;
char *head = des;
char *end = --tmp;
while(head < end){
strTmp = *head;
*head = *end;
*end = strTmp;
head++;
end--;
}
return des;
}
int main(){
int src = 1234;
char * des;
des = int2str(src, des);//这里注意啊,需要把des传进去,不然int2str只能返回局部变量的指针,这种是错误的。
printf("%s\n", des);
}