|
【实训目的】字符串处理函数
【第 1 步】实训内容介绍
实训内容介绍
字符串处理函数
实训结束
【Jitor 校验第 1 步】我已阅读实训内容介绍 // 送分题。直接点击。
【第 2 步】字符串处理函数
1、本实训使用项目 cpp4 中的 cpp4code.cpp 源代码文件,如果不存在,则先创建项目,然后创建源代码文件。
2、将下述代码复制到 cpp4code 中:
#include <iostream.h>
#include <string.h> // 字符串处理函数需要加上这一行
void main(void) {
char str1[80]; // 最大字符长度79
char str2[80];
char str3[160]; // 预留足够的空间,便于后面的处理
cout << "输入两行字符: ";
cin.getline(str1, 80);
cin.getline(str2, 80);
cout << "str1 = {" << str1 << "}\n";
cout << "str2 = {" << str2 << "}\n";
// 在这里补写代码
}
补写代码,调用字符串处理函数,实现相应的功能。代码见简明教程【例4-12】第 1 步代码,运行结果如下:
输入两行字符: This is the first line.
This is the second line.
str1 = {This is the first line.}
str2 = {This is the second line.}
str1的长度 = {23}
str2的长度 = {24}
比较str1和str2的大小 = {-1}
str3 = {This is the first line.}
str3 = {This is the first line.This is the second line.}
小写的str1 = {this is the first line.}
大写的str2 = {THIS IS THE SECOND LINE.}
Press any key to continue
代码如下:
// 从下一行开始复制。
#include <iostream.h>
#include <string.h> // 字符串处理函数需要加上这一行
void main(void) {
char str1[80]; // 最大字符长度79
char str2[80];
char str3[160]; // 预留足够的空间,便于后面的处理
cout << "输入两行字符: ";
cin.getline(str1, 80);
cin.getline(str2, 80);
cout << "str1 = {" << str1 << "}\n";
cout << "str2 = {" << str2 << "}\n";
// 在这里补写代码
cout << "str1的长度 = {" << strlen(str1) << "}\n";
cout << "str2的长度 = {" << strlen(str2) << "}\n";
cout << "比较str1和str2的大小 = {" << strcmp(str1, str2) << "}\n";
strcpy(str3, str1);
cout << "str3 = {" << str3 << "}\n";
strcat(str3, str2);
cout << "str3 = {" << str3 << "}\n";
cout << "小写的str1 = {" << strlwr(str1) << "}\n";
cout << "大写的str2 = {" << strupr(str2) << "}\n";
}
// 一直复制到上一行结束。
【Jitor 校验第 2 步】 // 复制上面的代码到VC++ 6.0,保存并运行。点击。
【第 3 步】实训总结
本实训学习了下述字符串处理函数:
字符串长度函数:strlen
字符串比较函数:strcmp
字符串复制函数:strcpy
字符串连接函数:strcat
转大写字母函数和转小写字母函数:strlwr 和strupr
【Jitor 校验第 3 步】我已阅读实训总结 // 送分题。直接点击。
仍有疑问 ? 联系QQ 9429444(陈海云) : 返回首页 |