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;
}

image.gif


目录
相关文章
|
2天前
|
C语言
PTA 浙大版《C语言程序设计(第3版)》题目集 习题8-4 报数 (20分)
PTA 浙大版《C语言程序设计(第3版)》题目集 习题8-4 报数 (20分)
|
2天前
|
C语言
C语言 浙大版《C语言程序设计(第3版)》题目集 练习8-8 移动字母 (10分)
C语言 浙大版《C语言程序设计(第3版)》题目集 练习8-8 移动字母 (10分)
|
2天前
|
C语言
浙大版《C语言程序设计(第3版)》题目集 练习8-2 计算两数的和与差 (10分)
浙大版《C语言程序设计(第3版)》题目集 练习8-2 计算两数的和与差 (10分)
|
2天前
|
C语言
pta浙大版《C语言程序设计(第3版)》 习题6-4 使用函数输出指定范围内的Fibonacci数 (20分)
pta浙大版《C语言程序设计(第3版)》 习题6-4 使用函数输出指定范围内的Fibonacci数 (20分)
|
2天前
|
C语言
PTA 浙大版《C语言程序设计(第3版)》题目集 习题8-6 删除字符 (20分)
PTA 浙大版《C语言程序设计(第3版)》题目集 习题8-6 删除字符 (20分)
|
2天前
|
C语言
pta 浙大版《C语言程序设计(第3版)》题目集 习题6-6 使用函数输出一个整数的逆序数 (20分)
pta 浙大版《C语言程序设计(第3版)》题目集 习题6-6 使用函数输出一个整数的逆序数 (20分)
|
2天前
|
C语言
(浙大版《C语言程序设计(第3版)》 习题6-5 使用函数验证哥德巴赫猜想 (20分)
(浙大版《C语言程序设计(第3版)》 习题6-5 使用函数验证哥德巴赫猜想 (20分)
|
C语言
《C语言程序设计》一 第 1 章 程序设计概述
本节书摘来自华章出版社《C语言程序设计》一 书中的第1章,第1.1节,作者:赵宏 陈旭东 马迪芳,更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1103 0
|
C语言
《C语言程序设计与实践(第2版)》——第1章 C语言与程序设计概述 1.1初见C语言程序
我国古代数学家张邱建在其编写的《算经》里提出了历史上著名的“百钱买百鸡”问题:今有鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。凡百钱买鸡百只,问鸡翁、母、雏各几何?对于这个问题,很多读者在小学或初中的竞赛中可能都见到过,而且通常都采用不定方程求解。
1108 0
|
5天前
|
C语言
C语言:内存函数(memcpy memmove memset memcmp使用)
C语言:内存函数(memcpy memmove memset memcmp使用)