C++从入门到放弃--13.字符串和关于字符串的函数

Hello~今天来更新

目录

1.使用

1.1.字面字符串

1.2.字符串

1.3.神奇的事情

2.字符串的I/O

2.1.putchar()

2.2.getchar()

2.3.puts()和gets()

2.4.fgets()和fputs()

2.5.strlen()

2.6.strcat()

2.7.strncat()

2.8.strcmp()

2.9.strncmp()

2.10.strcpy()

2.11.strncpy()


1.使用

1.1.字面字符串

字符串,就是把一堆字符串起来的一段字,叫字符串。

别给我说你没用过字符串,看:

cout << "Hello! I'm a string!" << "I'm a string, too.";

代码高亮的就叫字符串(我这是绿色):

 因为cout德才兼备,所以它能识别字符串。

那么,我们这叫字面字符串,就是你直接加双引号打上去的,看下面字符串:

"Hello"

占用六个字节。

6!

对,我没数错,看在内存中占用:

 最后的\0标志着字符串的结尾,如果没有\0,那就叫一堆char。

1.2.字符串

我们可以把字符串存在数组里:

char hello[10] = "Hello";

用""初始化后面就不用加\0了,或者如果你乐意,下面的也行:

char hello[10] = {'H', 'e', 'l', 'l', 'o', '\0'};

但是下面的不行(叫数组,不是字符串):

char hello[10] = {'H', 'e', 'l', 'l', 'o'};  //不是正确的字符串

所以在用char数组初始化字符串的时候一定要多加(至少)1个下标。

可以在前面加const,就是一个不可修改的字符串。

const char hello[10] = "Hello";  //不可修改

1.3.神奇的事情

emm,"Hello"这个字符串其实指的是它存放的地址,当做一个地址,用*可以引出第一个字符:

cout << *"space";

输出:

s

2.字符串的I/O

先说两个不关于字符串的函数:

2.1.putchar()

输出一个字符:

putchar('A');

输出:

A

ASCII码也可以:

putchar((char)32);

输出:

 

(空格)

2.2.getchar()

输入一个字符:

char c;
getchar(c);

getchar()和putchar()它俩跟printf()和scanf()一个系列,输入输出还是非常高效的。

2.3.puts()和gets()

puts()和gets()是比较糟糕的用来输出和输入字符串的函数,

(puts()在输出字符串完直接换行)

来看一个栗子:

char words[50] = "Hello! Please enter a string, please.";
puts(words);
gets(words);
puts("you entered:");
puts(word);

 输出如下:

Hello! Please enter a string, please.
I just think I'm safe.  //输入的
you entered:
I just think I'm safe.

这种情况下,puts()比较安全,但是下标溢出的时候,情况可就没有那么简单了。

char words[50] = "Hello! Please enter a string, please.";
puts(words);
gets(words);
puts("you entered:");
puts(word);

可以看到,main()的返回值都不对,“3221225477”,这种情况叫缓冲区溢出(buffer overflow),我输入的balalalalalala......明显超出了50的范围。

2.4.fgets()和fputs()

替代品,同样是上面的程序:

char words[50] = "Hello! Please enter a string, please.\n";
fputs(words, stdout);
fgets(words, 50, stdin);
fputs("you entered:", stdout);
fputs(words, stdout);
Hello! Please enter a string, please.
Oh! you use fgets() and fputs().              //输入的
you entered:Oh! you use fgets() and fputs().

fgets()和fputs()不会在末尾换行,而且fputs()还要多打一个stdout,fgets()还要多打stdin和上限(50)。

但是,安全!

同样的情境:

 可以看到,没有错误,只是后面的没了。

2.5.strlen()

(使用前请加上#include <cstring>)

把你的字符串丢给它,它返回你的字符串长度(带\0)。

可以改造上面的程序:

char words[50] = "Hello! Please enter a string, please.\n";
fputs(words, stdout);
fgets(words, 50, stdin);
fputs("you entered:", stdout);
fputs(words, stdout);
cout << endl << "string length:" << strlen(words);
	char words[50] = "Hello! Please enter a string, please.\n";
	fputs(words, stdout);
	fgets(words, 50, stdin);
	fputs("you entered:", stdout);
	fputs(words, stdout);
	cout << endl << "string length:" << strlen(words);

2.6.strcat()

(使用前请加上#include <cstring>)

用于拼接字符串,跟猫没有任何关系。

char word1[100] = "给我再去相信的勇气,";
char word2[100] = "越过谎言去拥抱你";
cout << strcat(word1, word2);

输出:

给我再去相信的勇气,越过谎言去拥抱你

2.7.strncat()

(使用前请加上#include <cstring>)

strcat()的升级版本,不讲。

2.8.strcmp()

(使用前请加上#include <cstring>)

比较两个字符串是否相同。

cout << "An ______ a day, keep the doctors away!(please enter the correct word)";
char answer[100];
fgets(answer, 100, stdin);
if (strcmp(answer, "apple") == 0 || strcmp(answer, "apples") == 0) {
    cout << "Yes!";
}
else {
    cout << "No!";
}

两次运行如下:

An ______ a day, keep the doctors away!(please enter the correct word)
apple  //输入
Yes!
An ______ a day, keep the doctors away!(please enter the correct word)
apples
Yes!

这个程序有个缺陷。

2.9.strncmp()

(使用前请加上#include <cstring>)

strcmp()的升级版本,不讲。

2.10.strcpy()

(使用前请加上#include <cstring>)

复制两个字符串。

char word1[100] = "Be the best that you can be.";
char word2[100];
cout << "You say:" << word1	<< endl;
strcpy(word1, word2);
cout << "I say:" << word2;
You say:Be the best that you can be.
I say:'@

乱码了!

具体我也不知道怎么回事。

2.11.strncpy()

(使用前请加上#include <cstring>)

strcpy()的升级版本,不讲。

拜~哪位帮我看一下,怎么乱码了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值