OJ 系列之字符串基本操作
2016-01-15 14:09
459 查看
字符串操作C语言提供了较多的库函数,本题目要求代码中不能使用字符串操作相关的库函数,可以使用malloc。
用例中可以使用中提供的库函数。
实现接口,每个接口实现1个基本操作:
unsigned int strlenth(char*s):计算字符串的长度。
void strcopy(char**target,char*source):字符串拷贝,从source拷贝到target中。
int strcompare(char*s,char*t):字符串比较,比较的原则:
对两个字符串的每个字符从左到右逐个比较,进行ASCⅡ比较,只要一个字符大,则整个字符大;
如果每个字符相等,但是其中一个字符串长度更长,则此字符串更大。
如果s>t,返回1;如果s==t,返回0;如果s<t,返回-1。<>
void strcombine(char**x,char*s,char*t):字符串连接,将字符串t接到s后面,并存储到x中。
void strcatch(char*s,unsignedintindex,unsignedintlenth,char**t):字符串截取,对字符串s中,从第index个字符开始,截取lenth长度的字符串,并把截取出来的字符串存储到t中。
bool strsubstr(char*s,char*sub):子串查找,在字符串s中查找字符串sub是否存在,如果存在则返回1,否则返回0。
题目框架中有17个参考用例,其它用例请执行编写。
/******************************************************************************
Copyright (C), scut.
******************************************************************************
File Name :
Version :
Author : ZP1015
Created : 2016/01/15
Last Modified :
Description :
Function List :
History :
1.Date : 2016/01/15
Author : ZP1015
Modification: Created file
******************************************************************************/
#include <stdlib.h>
unsigned int strlenth(char *s) /* 获取字符串长度 */
{
/* 1.异常处理 */
if(NULL == s)
return 0;
unsigned int lenth = 0;
while(*s != '\0') {
lenth ++;
s++ ;
}
return lenth;
}
void strcopy(char **target, char *source) /* 字符串拷贝 */
{
/* 1.异常处理 */
unsigned int length = strlenth(source);
*target = (char*)malloc(length+1);
**target = 0;
if(!source || !*target){
return;
}
unsigned int i = 0;
while(source[i]){
(*target)[i] = source[i++];
}
(*target)[i] = '\0';
}
/* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
int strcompare(char *s, char *t)
{
/*1.异常处理 */
if(!s && !t) /*都为空字符串*/
return 0;
if (!s && t)
return -1;
else if(!t && s)
return 1;
while(*s !='\0'&&*t !='\0') {
if(*s >*t)
return 1;
else if(*s <*t)
return -1;
t++;
s++;
}
if(*s == '\0'&&*t=='\0')
return 0;
else if(*s =='\0')
return -1;
return 1; /**t == '\0'*/
}
/* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
void strcombine(char **x, char *s, char *t)
{
/* 1.异常处理 */
unsigned int length = strlenth(s)+strlenth(t);
*x = (char *)malloc(length+1);
if(!x||!*x)
return;
unsigned int i = 0;
unsigned int j = 0;
while(!s && s[i]) {
(*x)[i] = s[i];
i ++;
}
j = i;
i = 0;
while(!t && t[i]) {
(*x)[j] = t[i];
i ++;
j ++;
}
(*x)[j] = '\0';
}
void strcombine(char **x, char *s, char *t)
{
if(!s){
*s = '\0';
}
if(!t){
*t = '\0';
}
unsigned int length = strlenth(s) + strlenth(t);
*x = (char*)malloc(length + 1);
**x = '\0';
unsigned int i = 0;
while(*s){
(*x)[i++] = *(s++);
}
while(*t){
(*x)[i++] = *(t++);
}
(*x)[i] = '\0';
}
/* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
void strcatch(char *s, unsigned int index, unsigned int lenth, char **t)
{
/* 1.异常处理 */
unsigned int length = strlenth(s);
if(lenth > length - index || lenth < 1 || index > length){
return;
}
*t = (char *)malloc(lenth+1);
**t = 0;
if(!s||!t||!*t)
return;
unsigned int i = index, j= 0;
for(; i <index + lenth; i++) {
(*t)[j++] = s[i];
}
(*t)[j] = '\0';
}
/* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
bool strsubstr(char *s, char *sub)
{
/* 1.异常处理 */
bool result = 0;
if(!s ||!sub)
return 0;
unsigned int sLen = strlenth(s);
unsigned int subLen = strlenth(sub);
if(sLen == 0 && subLen == 0)
return 1;
unsigned int i= 0,j=0;
for(i = 0;i < sLen;i++) {
if(s[i] == sub[j++]) {
if(j == subLen) {
return 1;
}
continue;
} else {
j = 0;
}
}
return 0;
}
用例中可以使用中提供的库函数。
实现接口,每个接口实现1个基本操作:
unsigned int strlenth(char*s):计算字符串的长度。
void strcopy(char**target,char*source):字符串拷贝,从source拷贝到target中。
int strcompare(char*s,char*t):字符串比较,比较的原则:
对两个字符串的每个字符从左到右逐个比较,进行ASCⅡ比较,只要一个字符大,则整个字符大;
如果每个字符相等,但是其中一个字符串长度更长,则此字符串更大。
如果s>t,返回1;如果s==t,返回0;如果s<t,返回-1。<>
void strcombine(char**x,char*s,char*t):字符串连接,将字符串t接到s后面,并存储到x中。
void strcatch(char*s,unsignedintindex,unsignedintlenth,char**t):字符串截取,对字符串s中,从第index个字符开始,截取lenth长度的字符串,并把截取出来的字符串存储到t中。
bool strsubstr(char*s,char*sub):子串查找,在字符串s中查找字符串sub是否存在,如果存在则返回1,否则返回0。
题目框架中有17个参考用例,其它用例请执行编写。
/******************************************************************************
Copyright (C), scut.
******************************************************************************
File Name :
Version :
Author : ZP1015
Created : 2016/01/15
Last Modified :
Description :
Function List :
History :
1.Date : 2016/01/15
Author : ZP1015
Modification: Created file
******************************************************************************/
#include <stdlib.h>
unsigned int strlenth(char *s) /* 获取字符串长度 */
{
/* 1.异常处理 */
if(NULL == s)
return 0;
unsigned int lenth = 0;
while(*s != '\0') {
lenth ++;
s++ ;
}
return lenth;
}
void strcopy(char **target, char *source) /* 字符串拷贝 */
{
/* 1.异常处理 */
unsigned int length = strlenth(source);
*target = (char*)malloc(length+1);
**target = 0;
if(!source || !*target){
return;
}
unsigned int i = 0;
while(source[i]){
(*target)[i] = source[i++];
}
(*target)[i] = '\0';
}
/* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
int strcompare(char *s, char *t)
{
/*1.异常处理 */
if(!s && !t) /*都为空字符串*/
return 0;
if (!s && t)
return -1;
else if(!t && s)
return 1;
while(*s !='\0'&&*t !='\0') {
if(*s >*t)
return 1;
else if(*s <*t)
return -1;
t++;
s++;
}
if(*s == '\0'&&*t=='\0')
return 0;
else if(*s =='\0')
return -1;
return 1; /**t == '\0'*/
}
/* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
void strcombine(char **x, char *s, char *t)
{
/* 1.异常处理 */
unsigned int length = strlenth(s)+strlenth(t);
*x = (char *)malloc(length+1);
if(!x||!*x)
return;
unsigned int i = 0;
unsigned int j = 0;
while(!s && s[i]) {
(*x)[i] = s[i];
i ++;
}
j = i;
i = 0;
while(!t && t[i]) {
(*x)[j] = t[i];
i ++;
j ++;
}
(*x)[j] = '\0';
}
void strcombine(char **x, char *s, char *t)
{
if(!s){
*s = '\0';
}
if(!t){
*t = '\0';
}
unsigned int length = strlenth(s) + strlenth(t);
*x = (char*)malloc(length + 1);
**x = '\0';
unsigned int i = 0;
while(*s){
(*x)[i++] = *(s++);
}
while(*t){
(*x)[i++] = *(t++);
}
(*x)[i] = '\0';
}
/* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
void strcatch(char *s, unsigned int index, unsigned int lenth, char **t)
{
/* 1.异常处理 */
unsigned int length = strlenth(s);
if(lenth > length - index || lenth < 1 || index > length){
return;
}
*t = (char *)malloc(lenth+1);
**t = 0;
if(!s||!t||!*t)
return;
unsigned int i = index, j= 0;
for(; i <index + lenth; i++) {
(*t)[j++] = s[i];
}
(*t)[j] = '\0';
}
/* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
bool strsubstr(char *s, char *sub)
{
/* 1.异常处理 */
bool result = 0;
if(!s ||!sub)
return 0;
unsigned int sLen = strlenth(s);
unsigned int subLen = strlenth(sub);
if(sLen == 0 && subLen == 0)
return 1;
unsigned int i= 0,j=0;
for(i = 0;i < sLen;i++) {
if(s[i] == sub[j++]) {
if(j == subLen) {
return 1;
}
continue;
} else {
j = 0;
}
}
return 0;
}
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- 如何写好 C main 函数
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C语言自动生成enum值和名字映射代码
- 使用C语言判断英文字符大小写的方法
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- C语言中计算正弦的相关函数总结
- 使用C语言详解霍夫曼树数据结构
- 探讨C语言的那些小秘密之断言
- C语言实现BMP转换JPG的方法
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言查找数组里数字重复次数的方法