文章目录

  • ​​二、字符串的输入​​
  • ​​3.gets()的替代品​​
  • ​​(1)fgets()与gets()的区别​​
  • ​​(2)空字符和空指针的区别​​
  • ​​五、字符串函数​​
  • ​​1.strlen()函数:统计字符串的长度​​
  • ​​2.strcat()函数:拼接字符串​​
  • ​​3.strncat()函数:更安全​​
  • ​​4.strcmp()函数:持续到字符串的尾巴​​
  • ​​(1)字符串不能比较地址,只能比较字符串的内容​​
  • ​​(2)不要使用字符或者'a'来作为strcmp()的参数​​
  • ​​二维数组如何检测输入空行?​​
  • ​​5.strncmp()函数:可以比较字符串不同地方​​
  • ​​指针数组的运用!!​​
  • ​​6.strcpy()函数​​
  • ​​二维数组与字符串的结合使用!!​​
  • ​​字符指针的运用​​
  • ​​7.strncpy()函数:更安全​​
  • ​​同样使用二维数组,来操作字符串​​
  • ​​8.sprintf()函数​​
  • ​​9.常用字符串函数用法大总结​​

二、字符串的输入

3.gets()的替代品

(1)fgets()与gets()的区别

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_02


eg:

/*  fgets1.c  -- using fgets() and fputs() */
#include <stdio.h>
#define STLEN 14
int main(void)
{
char words[STLEN];

puts("Enter a string, please.");
fgets(words, STLEN, stdin);
printf("Your string twice (puts(), then fputs()):\n");
puts(words);
fputs(words, stdout);
puts("Enter another string, please.");
fgets(words, STLEN, stdin);
printf("Your string twice (puts(), then fputs()):\n");
puts(words);
fputs(words, stdout);
puts("Done.");

return 0;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_03


解释如下:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_04


eg:这是一个简单的循环,读入并显示用户输入的内容,直到fgets()读到文件结尾或空行 (即,首字符是换行符)

#include<iostream>
using namespace std;
/* fgets2.c -- using fgets() and fputs() */
#include <stdio.h>
#define STLEN 10
int main(void)
{
char words[STLEN];

puts("Enter strings (empty line to quit):");
while (fgets(words, STLEN, stdin) != NULL && words[0] != '\n')
fputs(words, stdout);
puts("Done.");


cin.get();

return 0;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_05


解释如下:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_06


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_07


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_08

eg:在上面的例子基础上增加了代码,改程序读取输入行,删除存储存在字符串中的换行符,如果没有换行符,就丢弃数组装不下的字符。

/*  fgets3.c  -- using fgets() */
#include <stdio.h>
#define STLEN 10
int main(void)
{
char words[STLEN];
int i;

puts("Enter strings (empty line to quit):");
while (fgets(words, STLEN, stdin) != NULL
&& words[0] != '\n')
{
i = 0;
while (words[i] != '\n' && words[i] != '\0')
i++;
if (words[i] == '\n')
words[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
puts(words);
}
puts("done");
return 0;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_09


解释如下:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_10

(2)空字符和空指针的区别

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_11

五、字符串函数

1.strlen()函数:统计字符串的长度

(1)eg:

/* test_fit.c -- try the string-shrinking function */
#include <stdio.h>
#include <string.h> /* contains string function prototypes */
void fit(char *, unsigned int);

int main(void)
{
char mesg[] = "Things should be as simple as possible,"
" but not simpler.";

puts(mesg);
fit(mesg,38);
puts(mesg);
puts("Let's look at some more of the string.");
puts(mesg + 39);

return 0;
}

void fit(char *string, unsigned int size)
{
if (strlen(string) > size)
string[size] = '\0';
}

解释:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_12


(2)其他eg

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_13

2.strcat()函数:拼接字符串

(1)strcat()函数说明

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_14


(2)代码如下

/* str_cat.c -- joins two strings */
#include <stdio.h>
#include <string.h> /* declares the strcat() function */
#define SIZE 80
char * s_gets(char * st, int n);
int main(void)
{
char flower[SIZE];
char addon[] = "s smell like old shoes.";

puts("What is your favorite flower?");
if (s_gets(flower, SIZE))
{
strcat(flower, addon);
puts(flower);
puts(addon);
}
else
puts("End of file encountered!");
puts("bye");


return 0;
}


char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);//fgets()读取整行,包括换行符
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')//把换行符换成空字符\0
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'、、抛弃>=n的字符
while (getchar() != '\n')
continue;
}
return ret_val;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_15

3.strncat()函数:更安全

(1)strncat()函数解释

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_16


(2)eg:计算avaiable变量的值,用于表示允许添加的最大字符数

/* join_chk.c -- joins two strings, check size first */
#include <stdio.h>
#include <string.h>
#define SIZE 30
#define BUGSIZE 13
char * s_gets(char * st, int n);
int main(void)
{
char flower[SIZE];
char addon[] = "s smell like old shoes.";
char bug[BUGSIZE];
int available;

puts("What is your favorite flower?");
s_gets(flower, SIZE);
if ((strlen(addon) + strlen(flower) + 1) <= SIZE)
strcat(flower, addon);
puts(flower);
puts("What is your favorite bug?");
s_gets(bug, BUGSIZE);
available = BUGSIZE - strlen(bug) - 1;//13-5-1=7
strncat(bug, addon, available);
puts(bug);

return 0;
}
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_17

4.strcmp()函数:持续到字符串的尾巴

(1)字符串不能比较地址,只能比较字符串的内容

错误程序如下:

/* nogo.c -- will this work? */
#include <stdio.h>
#define ANSWER "Grant"
#define SIZE 40
char * s_gets(char * st, int n);

int main(void)
{
char try[SIZE];

puts("Who is buried in Grant's tomb?");
s_gets(try, SIZE);
while (try != ANSWER)
{
puts("No, that's wrong. Try again.");
s_gets(try, SIZE);
}
puts("That's right!");

return 0;
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

正确的程序如下:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_18


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_19

/* compare.c -- this will work */
#include <stdio.h>
#include <string.h> // declares strcmp()

#define ANSWER "Grant"
#define SIZE 40
char * s_gets(char * st, int n);

int main(void)
{
char try[SIZE];

puts("Who is buried in Grant's tomb?");
s_gets(try, SIZE);
while (strcmp(try,ANSWER) != 0)
{
puts("No, that's wrong. Try again.");
s_gets(try, SIZE);
}
puts("That's right!");

return 0;
}


char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

(2)不要使用字符或者’a’来作为strcmp()的参数

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_20


eg:

/* quit_chk.c -- beginning of some program */
#include <stdio.h>
#include <string.h>
#define SIZE 80
#define LIM 10
#define STOP "quit"
char * s_gets(char * st, int n);

int main(void)
{
char input[LIM][SIZE];
int ct = 0;

printf("Enter up to %d lines (type quit to quit):\n", LIM);
while (ct < LIM && s_gets(input[ct], SIZE) != NULL &&
strcmp(input[ct],STOP) != 0)
{
ct++;
}
printf("%d strings entered\n", ct);

return 0;
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

解释如下:

二维数组如何检测输入空行?

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_21

5.strncmp()函数:可以比较字符串不同地方

指针数组的运用!!

(1)eg:要查找以“”astro“开头的字符串,可以限定函数只查找这5个字符串

/* starsrch.c -- use strncmp() */
#include <stdio.h>
#include <string.h>
#define LISTSIZE 6
int main()
{
const char * list[LISTSIZE] =
{
"astronomy", "astounding",//有6个指针,指向不同的字符串
"astrophysics", "ostracize",
"asterism", "astrophobia"
};
int count = 0;
int i;

for (i = 0; i < LISTSIZE; i++)
if (strncmp(list[i],"astro", 5) == 0)
{
printf("Found: %s\n", list[i]);
count++;
}
printf("The list contained %d words beginning"
" with astro.\n", count);

return 0;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_22

6.strcpy()函数

二维数组与字符串的结合使用!!

(1)strcpy()函数相当于字符串赋值运算符

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_23

/* copy1.c -- strcpy() demo */
#include <stdio.h>
#include <string.h> // declares strcpy()
#define SIZE 40
#define LIM 5
char * s_gets(char * st, int n);

int main(void)
{
char qwords[LIM][SIZE];
char temp[SIZE];
int i = 0;

printf("Enter %d words beginning with q:\n", LIM);
while (i < LIM && s_gets(temp, SIZE))
{
if (temp[0] != 'q')
printf("%s doesn't begin with q!\n", temp);
else
{
strcpy(qwords[i], temp);
i++;
}
}
puts("Here are the words accepted:");
for (i = 0; i < LIM; i++)
puts(qwords[i]);

return 0;
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_24


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_25


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_26


(2)strcpy()函数的其他属性

  • strcpy()的返回类型是char *,该函数返回的是第1个参数的值,即一个字符的地址;
  • 第1个参数不必指向数组的开始;

字符指针的运用

eg:

/* copy2.c -- strcpy() demo */
#include <stdio.h>
#include <string.h> // declares strcpy()
#define WORDS "beast"
#define SIZE 40

int main(void)
{
const char * orig = WORDS;
char copy[SIZE] = "Be the best that you can be.";
char * ps;

puts(orig);
puts(copy);
ps = strcpy(copy + 7, orig);
puts(copy);
puts(ps);

return 0;
}

解释:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_27

7.strncpy()函数:更安全

同样使用二维数组,来操作字符串

(1)

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_28


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_29


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_30

/* copy3.c -- strncpy() demo */
#include <stdio.h>
#include <string.h> /* declares strncpy() */
#define SIZE 40
#define TARGSIZE 7
#define LIM 5
char * s_gets(char * st, int n);

int main(void)
{
char qwords[LIM][TARGSIZE];
char temp[SIZE];
int i = 0;

printf("Enter %d words beginning with q:\n", LIM);
while (i < LIM && s_gets(temp, SIZE))
{
if (temp[0] != 'q')
printf("%s doesn't begin with q!\n", temp);
else
{
strncpy(qwords[i], temp, TARGSIZE - 1);
qwords[i][TARGSIZE - 1] = '\0';
i++;
}
}
puts("Here are the words accepted:");
for (i = 0; i < LIM; i++)
puts(qwords[i]);

return 0;
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_31

8.sprintf()函数

(1)sprintf()的功能

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_32


(2)eg,程序说明如下:

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_33

/* format.c -- format a string */
#include <stdio.h>
#define MAX 20
char * s_gets(char * st, int n);

int main(void)
{
char first[MAX];
char last[MAX];
char formal[2 * MAX + 10];
double prize;

puts("Enter your first name:");
s_gets(first, MAX);
puts("Enter your last name:");
s_gets(last, MAX);
puts("Enter your prize money:");
scanf("%lf", &prize);
sprintf(formal, "%s, %-19s: $%6.2f\n", last, first, prize);
puts(formal);

return 0;
}

char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;

ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#define_34

9.常用字符串函数用法大总结

《C Primer Plus》学习笔记——第十一章字符串和字符串函数_#include_35


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_36


《C Primer Plus》学习笔记——第十一章字符串和字符串函数_字符串_37