题目:字符串常用操作,包括:字符串长度、比较、拷贝、连接、子串查找、子串截取(持续增加....)
分析:主要考察指针操作,小心边界溢出、越界等问题
代码:
-
/******************************************************************************
-
-
Copyright (C), 2001-2011, HW Tech. Co., Ltd.
-
-
******************************************************************************
-
File Name :
-
Version :
-
Author :
-
Created : 2012/03/12
-
Last Modified :
-
Description :
-
Function List :
-
-
History :
-
1.Date : 2012/03/12
-
Author :
-
Modification: Created file
-
-
******************************************************************************/
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
-
unsigned int strlenth(char *s) /* 获取字符串长度 */
-
{
-
unsigned int lenth = 0;
-
-
if (NULL == s)
-
return lenth;
-
-
// 代码在这里实现
-
char* tmp = s;
-
while (*tmp != '\0')
-
{
-
lenth ++;
-
tmp++;
-
}
-
-
return lenth;
-
}
-
-
void strcopy(char **target, char *source) /* 字符串拷贝 */
-
{
-
/* 代码在这里实现 */
-
if ( (NULL == source) || (NULL == target))
-
return;
-
-
char* dest;
-
char* src = source;
-
int len = strlenth(source);
-
dest = (char*)malloc(len+1);
-
if (NULL == dest)
-
return;
-
memset( dest,'\0',(len+1) );
-
*target = dest;
-
-
while (*src != '\0')
-
{
-
*dest = *src;
-
src++;
-
dest++;
-
}
-
-
return;
-
}
-
-
int strcompare(char *s, char *t) /* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
-
{
-
/* 代码在这里实现 */
-
if ( (NULL == s) || (NULL == t))
-
return 0;
-
-
char* str1 = s;
-
char* str2 = t;
-
-
while ( (*str1 != '\0') && (*str2 != '\0'))
-
{
-
if (*str1 > *str2)
-
return 1;
-
else if (*str1 < *str2)
-
return -1;
-
else
-
{
-
str1++;
-
str2++;
-
}
-
}
-
if ( (*str1 == '\0') && (*str2 == '\0') )
-
return 0;
-
else if ( (*str1 == '\0') && (*str2 != '\0' ) )
-
return -1;
-
else if ( (*str1 != '\0') && (*str2 == '\0') )
-
return 1;
-
-
return 0;
-
}
-
-
void strcombine(char **x, char *s, char *t) /* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
-
{
-
/* 代码在这里实现 */
-
if ( (NULL == s) || (NULL == t) || (NULL == x))
-
return ;
-
-
int len_s = strlenth(s);
-
int len_t = strlenth(t);
-
char* dest;
-
dest = (char*)malloc(len_s + len_t + 1);
-
if (NULL == dest)
-
return ;
-
memset(dest,'\0',(len_s + len_t));
-
-
*x = dest;
-
-
char *tmp = s;
-
while (*tmp != '\0')
-
{
-
*dest = *tmp;
-
tmp++;
-
dest++;
-
}
-
tmp = t;
-
while (*tmp != '\0')
-
{
-
*dest = *tmp;
-
tmp++;
-
dest++;
-
}
-
*dest = '\0';
-
}
-
-
void strcatch(char *s, unsigned int index, unsigned int lenth, char **t) /* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
-
{
-
/* 代码在这里实现 */
-
unsigned int len = strlenth(s);
-
if ( (NULL == s) || (len<=0) || (index<0) || (index>=len) || (NULL == t) || (lenth<=0) || (index+lenth>len) )
-
return ;
-
-
char *dest;
-
dest = (char*)malloc(index+1);
-
if (NULL == dest)
-
return ;
-
-
*t = dest;
-
unsigned int count = 0;
-
char *tmp = s;
-
while ( (*tmp != '\0') && (count < index) )
-
{
-
count++;
-
tmp++;
-
}
-
while ( (*tmp != '\0') && (lenth > 0) )
-
{
-
*dest = *tmp;
-
dest++;
-
tmp++;
-
lenth--;
-
}
-
*dest = '\0';
-
}
-
-
-
bool strsubstr(char *s, char *sub) /* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
-
{
-
bool result = 0;
-
-
/* 代码在这里实现 */
-
if ( (NULL == s) || (NULL == sub) || (strlenth(s)<strlenth(sub)) )
-
return 0;
-
char *tmp = s;
-
char *tmp_str;
-
-
while (*tmp != '\0')
-
{
-
tmp_str = sub;
-
while ( (*tmp != '\0') && (*tmp_str != '\0'))
-
{
-
if (*tmp == *tmp_str)
-
{
-
tmp++;
-
tmp_str++;
-
}
-
else
-
{
-
break;
-
}
-
}
-
if ( ( (*tmp == '\0') && (*tmp_str == '\0') ) || (*tmp!='\0') && (*tmp_str == '\0'))
-
{
-
result =1;
-
return result;
-
}
-
else
-
{
-
tmp++;
-
}
-
}
-
return result;
-
}
阅读(619) | 评论(0) | 转发(0) |