Week 7

7.1

字符串程序示例

#include <iostream>
#include <cstring> //包含字符串库函数的声明
using namespace std;
int main()
{
	char title[] = "Prison Break"; //title最后一个元素是'\0'
	char hero[100] = "Michael Scofield";
	char prisonName[100];
	char response[100];
	cout << "What's the name of the prison in " << title << endl;
	cin >> prisonName; //输入字符串
	if (strcmp(prisonName, "Fox-River") == 0) //字符串比较函数
		cout << "Yeah! Do you love " << hero << endl;
	else {
		//字符串拷贝函数
		strcpy(response, "It seems you haven't watched it!\n");
		cout << response;
	}
	title[0] = 't';
	title[3] = 0; //等效于 title [3] = '\0';
	cout << title << endl;
	return 0;
}

7.2 字符串的读入

scanf和cin会自动添加结尾的’\0’
scanf和cin读入到空格为止

cin.getline(char buf[], int bufSize);

读入一行(行长度不超过bufSize-1)或bufSize-1个字符到buf, 自动添加’\0’
回车换行符不会写入buf,但是会从输入流中去掉
例如:

char line[10]; 
cin.getline(line, sizeof(line)); 
//或 cin.getline(line,10); 读入最多9个字符到 line 
cout << line;
gets(char buf[]);

读入一行,自动添加’\0’
回车换行符不会写入buf,但是会从输入流中去掉。由于没有告知size,所以可能导致数组越界!

char s[10]; 
while( gets(s) ) 
{ 
	printf("%s\n",s); 
}

7.3 字符串库函数

在这里插入图片描述

字符串库函数用法示例

#include <iostream> 
#include <cstring> //要使用字符串库函数需要包含此头文件 
using namespace std; 

void PrintSmall( char s1[],char s2[]) //输出词典序小的字符串 
{ 
	if( strcmp( s1,s2) <= 0) //如果s1小于等于s2 
		cout << s1 ; 
	else cout << s2; 
} 

int main() 
{ 
	char s1[30]; 
	char s2[40]; 
	char s3[100]; 
	strcpy( s1,"Hello"); // 拷贝 "Hello" 到s1 , s1 = "Hello" 
	strcpy( s2,s1); // 拷贝s1到s2, s2 = "Hello" 
	cout << "1) " << s2 << endl; //输出 1) Hello 
	strcat( s1,",world"); // 连接 ",world"到s1尾部。s1 = "Hello,world" 
	cout << "2) " << s1 << endl; //输出 2) Hello,world
	cout << "3) "; PrintSmall("abc", s2); cout << endl; //输出 3) Hello
	cout << "4) "; PrintSmall("abc", "aaa"); cout << endl; //输出 4) aaa
	int n = strlen(s2); //求s2长度
	cout << "5) " << n << "," << strlen("abc") << endl; //输出 5) 5,3
	strupr(s1); // 把s1变成大写,s1 = "HELLO,WORLD"
	cout << "6) " << s1 << endl; //输出 6) HELLO,WORLD
	return 0;
}

在这里插入图片描述
解决方法:应取出s的长度存放在一个变量里面,然后在循环的时候使用该变量

char s[100] = "test"; 
int len = strlen(s); 
for (int i = 0; i < len; ++i) 
{ 
	s[i] = s[i] + 1; 
}

char s[100] = "test"; 
for (int i = 0; s[i]; ++i) 
{ 
	s[i] = s[i] + 1; 
}

例题:编写判断子串的函数

编写一个函数:
int Strstr(char s1[],char s2[]);
如果s2不是s1的字串,返回 -1
如果s2是s1的子串,返回其在s1中第一次出现的位置
空串是任何串的子串,且出现位置为0

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAX = 10000;
char s1[MAX];
char s2[MAX];

int Strstr(char s1[], char s2[])
{
	if (s2[0] == 0)	//空子串
		return 0;
	for (int i = 0; s1[i]; ++i)
	{
		int k = i, j = 0;
		for (; s2[j]; ++k, ++j)	//当s2没有了之后不会进入循环
		{
			if (s2[j] != s1[k])
				break;
		}
		if (s2[j] == 0)	//最后如果s2走到\0就说明比较到了最后,算是子数组
			return i;
	}
	return -1;
}

int main()
{
	gets_s(s1);
	gets_s(s2);
	cout << Strstr(s1, s2) << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值