数据结构重读 - 字符串基本操作

字符串(string)是由零个或者多个字符串组成的有限序列。

字符串中字符的数目称为字符串的长度

串中任意个连续字符组成的子序列称为改串的子串。包含子串的串相应地称为主串

串相等:当且仅当两个串的长度相等,并且各个对应位置的字符都相等时。

由一个或者多个空格组成的串'  '称为空格串,非空字符串!

C语言中的字符串最末尾是'\0',这个不用解释了。

串赋值StrAssign、串比较StrCompare、串求长StrLength、串连接StrConcat以及求子串SubString构成了字符串操作的最小操作子集。

字符串操作strcmp、strcmp、strcat、strlen操作的实现(不带n的非安全版本)。

#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
' && *p2!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { if(*p1>*p2) { return 1; }else if(*p1<*p2) { return -1; }else { p1++; p2++; } } // Length if(*p1=='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
' && *p2!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { return -1; }else if(*p1!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
' && *p2=='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { return 1; } else { return 0; } } void string_copy(char* src, char* dst) { while(*src!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { *dst++ = *src++; } *dst = '
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
'; } // Append s2 to s1 void string_concat(char* s1, char* s2) { // Go to end of s1 while(*s1!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { s1++; } // Copy s2 while(*s2!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { *s1++ = *s2++; } // End *s1 = '
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
'; } int string_length(char* s) { int len = 0; while(*s!='
#include <stdio.h>

int string_comp(char* s1, char* s2)
{
	char* p1 = s1;
	char* p2 = s2;	
	while(*p1!='\0' && *p2!='\0')
	{
		if(*p1>*p2)
		{
			return 1;
		}else if(*p1<*p2)
		{
			return -1;
		}else
		{
			p1++;
			p2++;
		}
	}
	// Length
	if(*p1=='\0' && *p2!='\0')
	{
		return -1;	
	}else if(*p1!='\0' && *p2=='\0')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void string_copy(char* src, char* dst)
{
	while(*src!='\0')
	{
		*dst++ = *src++;
	}
	*dst = '\0';
}

// Append s2 to s1
void string_concat(char* s1, char* s2)
{
	// Go to end of s1
	while(*s1!='\0')
	{
		s1++;
	}
	// Copy s2
	while(*s2!='\0')
	{
		*s1++ = *s2++;
	}
	// End
	*s1 = '\0';
}

int string_length(char* s)
{
	int len = 0;
	while(*s!='\0')
	{
		s++;
		len++;
	}
	return len;
}

int main()
{
	char* s1 = "Liheyuanxx";
	char* s2 = "liheyuan";
	char buf[1024];
	printf("string_comp:%d\n", string_comp(s1, s2));
	string_copy(s1, buf);
	printf("copy result:%s\n", buf);
	string_concat(buf, s2);
	printf("concat result:%s\n", buf);
	printf("length:%d\n", string_length(buf));
	return 0;
}
') { s++; len++; } return len; } int main() { char* s1 = "Liheyuanxx"; char* s2 = "liheyuan"; char buf[1024]; printf("string_comp:%d\n", string_comp(s1, s2)); string_copy(s1, buf); printf("copy result:%s\n", buf); string_concat(buf, s2); printf("concat result:%s\n", buf); printf("length:%d\n", string_length(buf)); return 0; }

Leave a Reply

Your email address will not be published. Required fields are marked *