C语言程序设计(王立柱)第六章答案 字符串

字符串函数汇总:

'\0'=0

'空格'=32

'0'~'9'=48~57

'A'~'Z'=65~90

'a'~'z'=97~122,'A'='a'-32

字符输入输出:

int getchar();//从键盘接受一个字符,返回字符的代码

int putchar(int c);//输出字符c,返回c的代码,最好每次调用putchar()函数前调用fflush(stdin)函数清空缓存区,stdin是符号化的缓冲区指针常量

字符串输入输出:

int puts(const char* s);//输出成功返回0

char* gets(char* s);//存入字符串或字符数组s,返回指针s

字符串求长:

int strlen(const char* s);

字符串复制:

char* strcpy(char* s1,const char* s2);//s2复制到s1,s1不能是字符串常量

char* strncpy(char* s1,const char* s2,int n);

使用函数时默认要求s1的空间足以容纳s2

字符串连接:

char* strcat(char* s1,const char* s2);

char* strncat(char* s1,const char* s2,int n);

字符串大小写:

char* strupr(char* s);//将字符串的小写字母改为大写字母,返回指针s,s不能表示字符串常量

char* strlwr(char* s);

字符串比较:

int strcmp(const char* s1,const char* s2);//从后往前比较,相等返回0,前大返回1,后大返回-1

int strncmp(const char* s1,const char* s2,int n);

字符查找:

const char* strchr(const char* s,int ch);

//在s中查找第一个出现的字符ch,返回字符指针,不存在时返回指针0

//int* a=0;指针赋0,就是不指向任何对象,相当于NULL

const char* strrchr(const char* s,int ch);//查找最后一个出现的ch

字符串匹配:

char* strstr(const char* str,const char* substr);

//在主串str中查找第一个出现的子串substr,返回子串在主串中的首字符指针,不存在时返回指针0

编程习题1:在字符串的基本函数方法设计中,大多使用索引方法,改用指针方法重新编写基本函数的代码

编程习题2:编写字符串匹配函数

这里在应用字符串匹配函数验证时有一点需要注意:因为验证了多种情况并使用Puts()输出Strstr()返回的指针,有一种情况是没有找到匹配的子串返回0指针,这时使用Puts()就会出现问题。不能直接用书上的代码,要加上指针为0时的情况,分开输出并返回值。因为对于零指针,不是字符串,也就不满足=='\0'的退出条件,一旦i++,就会超出访问权限。

//usersstring.h
#pragma once
#include<stdio.h>
//字符串输入
char* Gets(char* s) {
	char ch;
	int i = 0;
	ch = getchar();
	while (ch != '\0') {
		*(s + i) = ch;
		ch = getchar();
	}
	*(s + i) = '\0';
	return s;
}
//字符串输出
int Puts(const char* s) {
	if (s == 0) {
		printf("NULL!\n");
		return 0;
	}
	int i = 0;
	while (*(s + i) != '\0') {
		putchar(*(s + i));
		i++;
	}
	printf("\n");
	return 0;
}
//字符串长度
int Strlen(const char* s) {
	int i = 0;
	while (*(s + i) != '\0')
		i++;
	return i;
}
//字符串复制
char* Strcpy(char* s1, const char* s2) {
	int i = 0;
	while (*(s2 + i) != '\0') {
		*(s1 + i) = *(s2 + i);
		i++;
	}
	*(s1 + i) = '\0';
	return s1;
}
char* Strncpy(char* s1, const char* s2, int n) {
	int i;
	int len = Strlen(s1);
	for (i = 0; i < n; i++)
		*(s1 + i) = *(s2 + i);
	if (len < n)
		*(s1 + i) = '\0';
	return s1;
}
//字符串拼接
char* Strcat(char* s1, const char* s2) {
	int i = Strlen(s1);
	int j = 0;
	while (*(s2 + j) != '\0')
		*(s1 + (i++)) = *(s2 + (j++));
	*(s1 + i) = '\0';
	return s1;
}
char* Strncat(char* s1, const char* s2, int n) {
	int i = Strlen(s1);
	int j = 0;
	while (j < n) {
		*(s1 + i) = *(s2 + j);
		i++;
		j++;
	}
	*(s1 + i) = '\0';
	return s1;
}
//字符串大小写
char* Strupr(char* s) {
	int i = 0;
	while (*(s + i) != '\0') {
		if (*(s + i) > 96 && *(s + i) < 123)
			*(s + i) -= 32;
		i++;
	}
	return s;
}
char* Strlwr(char* s) {
	int i = 0;
	while (*(s + i) != '\0') {
		if (*(s + i) > 64 && *(s + i) < 91)
			*(s + i) += 32;
		i++;
	}
	return s;
}
//字符串比较
int Strcmp(const char* s1, const char* s2) {
	int i = 0;
	while (*(s1 + i) != '\0' && *(s2 + i) != '\0') {
		if (*(s1 + i) != *(s2 + i))
			break;
		i++;
	}
	if (*(s1 + i) == '\0' && *(s2 + i) == '\0')
		return 0;
	return *(s1 + i) > *(s2 + i) ? 1 : -1;
}
int Strncmp(const char* s1, const char* s2, int n) {
	int i = 0;
	while (i < n) {
		if (*(s1 + i) != *(s2 + i))
			break;
		i++;
	}
	if (i == n)
		return 0;
	return *(s1 + i) > *(s2 + i) ? 1 : -1;
}
//字符串查找字符(第一次出现,最后一次出现)
const char* Strchr(const char* s, int ch) {
	const char* p = s;
	while (*p != '\0') {
		if (*p == ch)
			return p;
		p++;
	}
	return 0;
}
const char* Strrchr(const char* s, int ch) {
	const char* p = s + Strlen(s) - 1;
	while (p != s) {
		if (*p == ch)
			return p;
		p--;
	}
	return 0;
}
//字符串查找字符串
const char* Strstr(const char* str, const char* substr) {
	const char* p = str;
	int i;
	while (*p != '\0') {
		for (i = 0; i < Strlen(substr); i++)
			if (p[i] != substr[i])
				break;
		if (i == Strlen(substr))
			return p;
		p++;
	}
	return 0;
}




//main.h
#include<stdio.h>
#include"usersstring.h"
int main() {
	//应用重新编写的字符串匹配函数
	const char* p1 = "china is a country";
	const char* p2 = "china";
	const char* p3 = "is";
	const char* p4 = "country";
	char p[20] = "yue";
	const char* p01 = Strstr(p1, p2);
	const char* p02 = Strstr(p1, p3);
	const char* p03 = Strstr(p1, p4);
	const char* p04 = Strstr(p1, p);
	Puts(p01);
	Puts(p02);
	Puts(p03);
	Puts(p04);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值