我是靠谱客的博主 冷傲柜子,最近开发中收集的这篇文章主要介绍C语言之字符串学习笔记,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

字符串分配内存的三种方式:堆上、栈上、全局常量区
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct CString
{
	char *p;
	int reallength;
};

typedef struct CString mystring;

void init(mystring *string);
void initwithlength(mystring *string, int length);
void initwithstring(mystring *string, char *copystring);
void printfstring(mystring *string);
void backaddchar(mystring *string, char ch);
void backaddstring(mystring *string, char *str);
void run(mystring *string);
char * findfirstchar(mystring *string, char ch);
char * findfirststring(mystring *string, const char *str);
int deletefirstchar(mystring *string, char ch);
int deletefirststring(mystring *string, const char *str);

void addchar(mystring *string, char ch, char *pos);
void addstring(mystring *string, char *str, char *pos);

void changefirstchar(mystring *string, const char oldchar, const char newchar);
void changefirststring(mystring *string, char * const oldstring, char * const newstring);


#include"字符串.h"

int mystrlen(char *p)
{
	if (p == NULL)
	{
		return -1;
	}
	int length = 0;
	while (*p != '')
	{
		length++;
		p++;
	}
	return length;
}

char *mystrcpy(char *dest, const char *source)
{
	if (dest == NULL || source == NULL)
	{
		return NULL;
	}
	char * destbak = dest;
	while (*source != '')
	{
		*dest = *source;
		source++;
		dest++;
	}
	*dest = '';
	return destbak;
}

char *mystrcat(char *dest, const char *source)
{
	if (dest == NULL || source == NULL)
	{
		return NULL;
	}
	else
	{
		char *destbak = dest;
		while (*dest != '')
		{
			dest++;
		}
		while (*source != '')
		{
			*dest=*source;
			dest++;
			source++;
		}
		*dest = '';
		return dest;
	}
}

char *mystrchr(const char *dest, const char ch)
{
	if (dest == NULL)
	{
		return NULL;
	}
	while (*dest != '')
	{
		if (*dest == ch)
		{
			return dest;
		}
		dest++;
	}
	return NULL;
}

char *mystrstr(const char * const dest, const char * const findstr)
{
	if (dest == NULL || findstr == NULL)
	{
		return NULL;
	}
	char *destbak = dest;
	char *p = NULL;
	while (*destbak != '')
	{
		int flag = 1;
		char *findstrbak = findstr;
		char *nowdestbak = destbak;
		while (*findstrbak!='')
		{
			if (*findstrbak != *nowdestbak)
			{
				flag = 0;
				break;
			}
			findstrbak++;
			nowdestbak++;
		}
		if (flag == 1)
		{
			return destbak;
		}
		destbak++;
	}
	return NULL;
}

void init(mystring *string)
{
	string->p = NULL;
	string->reallength = 0;
}

void initwithlength(mystring *string, int length)
{
	//string->p = (char *)malloc(sizeof(char)*length);
	string->p = (char *)calloc(length, sizeof(char));		//分配内存并清零
	string->reallength = length;
}

void initwithstring(mystring *string, char *copystring)
{
	int length = strlen(copystring);
	string->p = (char *)calloc(length+1, sizeof(char));		//分配内存并清零
	string->reallength = length+1;
	mystrcpy(string->p, copystring);
}

void backaddchar(mystring *string, char ch)
{
	if (mystrlen(string->p) + 1 == string->reallength)
	{
		string->p = (char*)realloc(string->p, string->reallength+1);
		string->reallength += 1;
		string->p[string->reallength - 2] = ch;
		string->p[string->reallength - 1] = '';
	}
	else
	{
		int nowlength = mystrlen(string->p);
		string->p[nowlength] = ch;
		string->p[nowlength+1] = '';
	}
}

void backaddstring(mystring *string, const char *str)
{
	int nowmystringlength = mystrlen(string->p);
	int addstringlength = mystrlen(str);
	if (nowmystringlength + addstringlength + 1>string->reallength)
	{
		int needaddlength = nowmystringlength + addstringlength + 1 - string->reallength;
		string->p = (char*)realloc(string->p,string->reallength+needaddlength);
		mystrcat(string->p, str);
		string->reallength += needaddlength;
	}
	else
	{
		mystrcat(string->p, str);
	}
}

void printfstring(mystring *string)
{
	printf("nstring=%s", string->p);
}

void run(mystring *string)
{
	system(string->p);
}

char * findfirstchar(mystring *string, char ch)
{
	char *p = mystrchr(string->p, ch);
	return p;
}

char * findfirststring(mystring *string, const char *str)
{
	char *pres = mystrstr(string->p, str);
	return pres;
}

int deletefirstchar(mystring *string, char ch)
{
	char *p = mystrchr(string->p, ch);
	if (p == NULL)
	{
		return 0;
	}
	else
	{
		char *pnext = p + 1;
		while (*pnext != '')
		{
			*p = *pnext;
			p++;
			pnext++;
		}
		*p = '';
		return 1;
	}
}

int deletefirststring(mystring *string, const char *str)
{
	char *pres = mystrstr(string->p, str);
	if (pres == NULL)
	{
		return 0;
	}
	else
	{
		int length = mystrlen(str);
		char *pnext = pres + length;
		while (*pnext != '')
		{
			*pres = *pnext;
			pres++;
			pnext++;
		}
		*pres = '';
		return 1;
	}
}

void addchar(mystring *string, char ch, char *pos)
{
	if (pos == NULL || string == NULL)
	{
		return;
	}
	if (mystrlen(string->p) + 1 == string->reallength)
	{
		string->p = (char*)realloc(string->p, string->reallength + 1);
		string->reallength += 1;

		int nowlength = mystrlen(string->p);
		int movelength = mystrlen(pos);
		for (int i = nowlength; i > nowlength - movelength; i--)
		{
			string->p[i] = string->p[i - 1];
		}
		string->p[nowlength - movelength] = ch;
		string->p[nowlength + 1] = '';
	}
	else
	{
		int nowlength = mystrlen(string->p);
		int movelength = mystrlen(pos);
		for (int i = nowlength; i > nowlength - movelength; i--)
		{
			string->p[i] = string->p[i - 1];
		}
		string->p[nowlength - movelength] = ch;
		string->p[nowlength + 1] = '';
	}
}

void addstring(mystring *string, char *str, char *pos)
{
	if (pos == NULL || string == NULL)
	{
		return;
	}
	int nowmystringlength = mystrlen(string->p);
	int addstringlength = mystrlen(str);
	if (nowmystringlength + addstringlength + 1>string->reallength)
	{
		int needaddlength = nowmystringlength + addstringlength + 1 - string->reallength;
		string->p = (char*)realloc(string->p, string->reallength + needaddlength);
		string->reallength += needaddlength;
		int nowlength = mystrlen(string->p);
		int movelength = mystrlen(pos);
		int insertlength = mystrlen(str);
		for (int i = nowlength; i >= nowlength - movelength; i--)
		{
			string->p[i+insertlength] = string->p[i];
		}
		for (int j = 0; j < insertlength; j++)
		{
			string->p[nowlength-movelength + j] = str[j];
		}
	}
	else
	{
		int nowlength = mystrlen(string->p);
		int movelength = mystrlen(pos);
		int insertlength = mystrlen(str);
		for (int i = nowlength; i >= nowlength - movelength; i--)
		{
			string->p[i + insertlength] = string->p[i];
		}
		for (int j = 0; j < insertlength; j++)
		{
			string->p[nowlength - movelength + j] = str[j];
		}
	}
}

void changefirstchar(mystring *string, const char oldchar, const char newchar)
{
	char *pstr = string->p;
	while (*pstr != '')
	{
		if (*pstr==oldchar)
		{
			*pstr = newchar;
			return;
		}
		pstr++;
	}
}

void changefirststring(mystring *string, char * const oldstring, char * const newstring)
{
	char *pfind = findfirststring(string, oldstring);
	if (pfind != NULL)
	{
		deletefirststring(string, oldstring);
		addstring(string, newstring, pfind);
	}
}


最后

以上就是冷傲柜子为你收集整理的C语言之字符串学习笔记的全部内容,希望文章能够帮你解决C语言之字符串学习笔记所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部