文章目录
一. 字符串
1.1 基本概念
字符串是由多个字符组成,例如:“ascds”,“aa”,“ccc”,这些都是字符串。在C语言中,字符串都是存放在字符数组里面。
char str1[6] = "world";//(1)
char str2[6] = {
'w', 'o', 'r', 'l', 'd', '\0'};//(2)
并且字符串的末端都会有一个终止标识符’\0’,因为’\0’也是一个字符,所以也需要占一个字节。按照第一种写法,将一个字符串常量放入字符数组中,编译器会自动在末尾增加’\0’,按照第二种写法,则需自己添加,如果没有添加的话,编译器识别不到终止符,就会一直向后面的内存空间访问,每个字节的内存空间都会随机出现一些字符,直到识别到’\0’才会停止。
1.2 计算字符串的长度
在C语言中,有一个函数strlen(),是用来计算字符串长度的,引用的头文件为#include <string.h>
定义方式为int strlen(const char* str)
#include <stdio.h>
#include <string.h>
int main(){
char str1[6] = "world";
int n = strlen(str1);
printf("%d\n", n);//输出结果为5
return 0;
}
1.3 遍历方式
其遍历方式和数组一样,通过下标进行访问,使用循环,可以对每个内存空间进行访问,以终止标识符为结束条件。
#include <stdio.h>
#include <string.h>
int main(){
char str1[6] = "world";
for (int i = 0; i < strlen(str1); i++){
printf("%c ", str1[i]);
}
printf("\n");
return 0;
}
二. 课后习题
2.1 判断国际象棋棋盘中一个格子的颜色
1812. 判断国际象棋棋盘中一个格子的颜色
分析:
题目所给的条件中,是用字母a,b,c等来表示横坐标,用数字1,2,3等表示纵坐标,那我们是否可以考虑将横坐标转化为数字表示呢,很显然,我们可以通过ASCLL码表对其进行转化,int x = s[0] - ‘a’;这样很坐标就转化为由0,1,2表示了。
转化后我们发现,当x + y为奇数时,其对应的方块为黑色,为偶数时,其对印的方块为白色,得到这样的关系后,问题便迎刃而解了。
代码如下:
bool squareIsWhite(char * coordinates){
int x = coordinates[0] - 'a';
int y = coordinates[1] - '1';
if((x + y) % 2 == 0){
return false;
}
return true;
}
2.2 速算机器人
LCP 17. 速算机器人
这道太简单了,根本不需要思考,直接上手写代码:
int A(int x, int y){
return 2 * x + y;
}
int B(int x, int y){
return 2 * y + x;
}
int calculate(char* s){
int x = 1, y = 0;
for(int i = 0; i < strlen(s); i++){
if(s[i] == 'A'){
x = A(x, y);
}
else if(s[i] == 'B'){
y = B(x, y);
}
}
return x + y;
}
2.3 执行操作后的变量值
2011.执行操作后的变量值
这题也很简单,不分析了,上代码:
int finalValueAfterOperations(char ** operations, int operationsSize){
char op1[] = "--X";
char op2[] = "++X";
char op3[] = "X++";
char op4[] = "X--";
int x = 0;
for(int i = 0; i < operationsSize; i++){
if(strcmp(operations[i], op1) == 0 || strcmp(operations[i], op4)==0){
x--;
}
else if(strcmp(operations[i], op2)==0 || strcmp(operations[i], op3)==0){
x++;
}
}
return x;
}
2.4 长度为三且各字符不同的子字符串
1876. 长度为三且各字符不同的子字符串
分析:这道题我们可以用双指针的方法解题
代码如下:
int countGoodSubstrings(char * s){
int ret = 0;
int len = strlen(s);
for(int i = 0; i < len - 2; i++){
if(s[i] != s[i + 2] && s[i] != s[i + 1] && s[i+1] != s[i + 2]){
ret++;
}
}
return ret;
}
2.5 1704. 判断字符串的两半是否相似
1704. 判断字符串的两半是否相似
很简单,代码如下:
bool halvesAreAlike(char * s){
int len = strlen(s);
int a = 0, b = 0;
for(int i = 0; i < len/2; i++){
int j = i + len / 2;
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){
a++;
}
if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u'||s[j]=='A'||s[j]=='E'||s[j]=='I'||s[j]=='O'||s[j]=='U'){
b++;
}
}
if(a==b){
return true;
}
return false;
}
2.6 将所有数字用字符替换
1844. 将所有数字用字符替换
这道题我们要注意,偶数位下标的对应的字符是字母,奇数位下标对应的字符为数字,根据这一点我们可以对其进行判断。
代码如下:
char shift(char ch, char i){
int n = i - '0';
char ret = ch + n;
return ret;
}
char * replaceDigits(char * s){
int n = strlen(s);
char* ret = malloc((n + 1)* 2);
int retSize = 0;
for(int i = 0; i < n; i++){
if(i % 2 == 0){
ret[retSize++] = s[i];
}
else{
ret[retSize++] = shift(s[i-1], s[i]);
}
}
ret[retSize] = '\0';
return ret;
}