2.字符串指针必须初始化,数组不用
2.1代码展示:
#include <iostream> using namespace std; int main() { char* s; //字符串指针变量 s = "hello"; //把字符串赋值给字符串指针,编译器会给字符串一个地址; cout<<"字符串的地址:"<<&s<<endl; //表示变量本身所在的内存单元地址 cout<<"字符串的地址:"<<s<<endl; //表示s的单元所保存的地址 cout<<"字符串指针的值为:"<<s[1]<<endl; //这里会输出字符串的首个元素,而不是整个字符串 cout<<"字符串指针分配的单元:"<<sizeof(s)<<endl; //用来保存地址的 return 0; }
2.2效果展示:
3.两个char*判断相等不相等(==)
3.1.代码展示:
#include <iostream> using namespace std; int main() { const char* s = "abc"; const char* s1 = "abc"; if (s == s1) { cout << "相等" << endl; } else cout << "不相等" << endl; return 0; }
3.2.效果展示:
4.两个string 可以用==
4.1代码展示:
#include <iostream> #include <string.h> using namespace std; int main() { string s = "abc"; string s1 = "abc"; if (s==s1) { cout << "相等" << endl; } else cout << "不相等" << endl; return 0; }
4.2效果展示:
5.一个char*与string可以用==
5.1代码展示:
#include <iostream> #include <string.h> using namespace std; int main() { string s = "abc"; const char* s1 = "abc"; if (s==s1) { cout << "相等" << endl; } else cout << "不相等" << endl; return 0; }
5.2效果展示:
4.两个char*也可以用strcmp
4.1代码展示:
#include <iostream> #include <string.h> using namespace std; int main() { const char* s = "abc"; const char* s1 = "abc"; if (strcmp(s ,s1)==0) { cout << "相等" << endl; } else cout << "不相等" << endl; return 0; }
4.2效果展示:
5.一个char*与一个string不可以用strcmp
5.1代码展示:
#include <iostream> #include <string.h> using namespace std; int main() { string s = "abc"; const char* s1 = "abc"; if (strcmp(s ,s1)==0) { cout << "相等" << endl; } else cout << "不相等" << endl; return 0; }
5.2效果展示:
6.字符串的结束以分号为单位:
#include <iostream> using namespace std; void main() { char s1[]="ABC" "DEF"; //********error******** char s2[]="ABC\ DEF"; //********error******** char s3[]="ABC" "DEF"; cout<<s1<<endl; cout<<s2<<endl; cout<<s3<<endl; return; } ———————————————— 版权声明:本文为CSDN博主「吉士先生.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_69683957/article/details/125842311
(五)、ASCii码的应用
1.什么是ASCii码
ASCII (American Standard Code for Information Interchange):美国信息交换标准代码是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言。它是最通用的信息交换标准,并等同于国际标准 ISO/IEC 646。ASCII第一次以规范标准的类型发表是在1967年,最后一次更新则是在1986年,到目前为止共定义了128个字符。
2.Ascii表的排序规则:
大小规则
常见ASCII码的大小规则:数字< 大写字母 < 小写字母。
1.数字比字母要小。如 “7”<“F”;
2.数字0比数字9要小,并按0到9顺序递增。如 “3”<“8” ;
3.字母A比字母Z要小,并按A到Z顺序递增。如“A”<“Z” ;
4.同个字母的大写字母比小写字母要小32。如“A”<“a” 。
几个常见字母的ASCII码大小: “A”为65;“a”为97;“0”为 48