c语言实现string(字符串)& 二级指针的用法

  • 接口参考严蔚敏老师的数据结构课本
  • 这个类bug de了好长时间,在于二级指针的使用(C语言学的不好用指针真的伤)
  • 实现环境为linux

下面先讲一点关于二级指针的知识点

void change(char** x)
{
    *x = "bbb";
}
 
int main(void)
{
    char *y = "aaa";
    change(&y);
   printf("%s",y);    
   return 0;
}

看一下这个函数,其输出为bbb;这是正确的结果

再看下面一个函数:

void change(char* x)
{
    *x = "bbb";
}
 
int main(void)
{
    char *y = "aaa";
    change(y);
   printf("%s",y);    
   return 0;
}

这个函数的输出为aaa,结果不是我们想要的

其实这个很容易理解,我们把char* 想象成string则二级指针则变成了string*,也就是指向字符串的一级指针

下面接着看String的实现:

String.h

#ifndef STRING_H_
#define STRING_H_
#include <stdbool.h>

#define ERROR 0
#define OK 1
#define OVERFLOW -1

typedef struct {
	char *ch;
	int length;
} String;

/*generate a string whose value is equal to chars*/
bool StrAssign(String *S, char **chars);

/*return the length of the string*/
unsigned int Length(String S);

/*compare S1 and S2 and if S1 > S2 return 1 else return 0*/
bool Compare(String S1, String S2);

/*empty the string*/
void ClearString(String *S);

/*concat S1 and S2 and return the value using T*/
bool Concat(String *S, String S1, String S2);

/*return a substring of S whose length is len from the position of pos*/
String SubString(String S, int pos, int len);

/*print the string*/
void Traverse(String S);
#endif

String.c

#include <stdio.h>
#include "String.h"
#include <stdlib.h>
#include <string.h>
bool StrAssign(String *S, char **chars) {
/*	if(S->ch) {
		S->length = 0;
		free(S->ch);
		S->ch = NULL;
	}*/
	int i;
	char *c;
	//i = strlen(*chars);
	for(i = 0, c = *chars; *c; i++, c++);
	//	printf("%d\n",i);
	if (!i) {
		S->ch = NULL;
		S->length = 0;
	}
	else {
		S->ch = (char*)malloc(i * sizeof(char));
		for(int j = 0; j < i; j++) {
			S->ch[j] = (*chars)[j];
		}
		S->length = i;
	}
	return OK;

}


unsigned int Length(String S) {
	return S.length;
}

/*compare S1 and S2 and if S1 > S2 return 1 else return 0*/
bool Compare(String S1, String S2) {
	int i, j;
	for (i = 0, j = 0; i < S1.length || i < S2.length; i++, j++) {
		if (S1.ch[i] > S2.ch[j]) {
			return 1;
		}
		else if (S1.ch[i] < S2.ch[j]) {
			return 0;
		}
		else {
			continue;
		}
	}
	if(S1.length > S2.length) 
		return 1;
	else
		return 0;
}
/*empty the string*/
void ClearString(String *S) {
	S->length = 0;
	free(S->ch);
	S->ch = NULL;
}

/*concat S1 and S2 and return the value using T*/
bool Concat(String *S, String S1, String S2) {
	S->ch = (char*)malloc((S1.length + S2.length) * sizeof(char));
	S->length = S1.length + S2.length;
	for (int i = 0; i < S1.length; i++) {
		S->ch[i] = S1.ch[i];
	}
	for (int i = 0; i < S2.length; i++) {
		S->ch[i + S1.length] = S2.ch[i];
	}
	return OK;	
}

/*return a substring of S whose length is len from the position of pos*/
String SubString(String S, int pos, int len) {
	String T;
	T.length = len;
	T.ch = (char*)malloc(len*sizeof(char));
	for (int i = pos - 1; i < pos - 1 + len; i++) {
		T.ch[i - pos + 1] = S.ch[i];
	}
	return T;
}

void Traverse(String S) {
	if (S.ch == NULL)
		printf("null");
	else {
		for (int i = 0; i < S.length; i++) {
			printf("%c", S.ch[i]);
		}
	}
	printf("\n");
} 

main.c

#include <stdio.h>
#include "String.h"
int main () {
	String S, S1, S2;
	char *m = "ef";
	StrAssign(&S1,&m);
	//printf("%d\n",S1.length);
	char *a = "abced";
	StrAssign(&S, &a);
	Traverse(S);
	//printf("%d\n",S.length);
	//printf("%p\n",m);
	//printf("%p\n",a);
	Traverse(S1);
	Concat(&S2, S, S1);
	Traverse(S2);
	String s = SubString(S2, 2, 3);	
	Traverse(s);
	if (Compare(S1, S2))
		printf("S1 > S2\n");
	else
		printf("S1 <= S2\n");
	ClearString(&S);
	ClearString(&s);
	ClearString(&S1);
	ClearString(&S2);
	Traverse(S);
}

makefile

object = main.o String.o

test : $(object)
	gcc -g -Wall -o test $(object)

main.o : String.h
String.o : String.h

.PHONY : clean

clean:
	rm -rf *.o
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值