@鞠大龙 魅客科创中心
char的本质:
字符常量:
char c1 = 'A'; // 字符形式 char c2 = 65; // 数字形式(ASCII码) char c3 = '\n'; // 转义字符
常用ASCII码:
ASCII码规律:
// 字母间转换 'a' - 'A' == 32 // 大小写差值 'b' - 'a' == 1 // 相邻字母差值 // 数字字符转整数 '5' - '0' == 5 // 字符转数字
// 字符与ASCII码转换 char ch = 'A'; int ascii = ch; // 字符转ASCII: 65 char back = (char)ascii; // ASCII转字符: 'A' // 大小写转换 char upper = 'a' - 32; // 小写转大写: 'A' char lower = 'A' + 32; // 大写转小写: 'a' // 数字字符转换 char digit = '7'; int num = digit - '0'; // 字符转数字: 7 char back_digit = num + '0'; // 数字转字符: '7'
// 定义方式 char str1[100]; // 未初始化 char str2[100] = ""; // 空字符串 char str3[] = "Hello"; // 自动计算长度 char str4[6] = "Hello"; // 指定长度(需要考虑\0) // 注意事项 char str5[5] = "Hello"; // 错误!没有空间存储\0 char str6[] = {'H','i'}; // 危险!没有\0结束符
结束符特点:
错误示例:
char s[5] = "Hello"; // 错误 // 需要6个字符空间: // H e l l o \0
正确使用:
char s[6] = "Hello"; // 内存布局: // s[0] s[1] s[2] s[3] s[4] s[5] // H e l l o \0 // 遍历到\0为止 int i = 0; while(s[i] != '\0'){ cout << s[i]; i++; }
char str[100]; // 输入方式 cin >> str; // 遇空格停止 cin.getline(str, 100); // 读取整行 gets(str); // 读取整行(不推荐) // 输出方式 cout << str; // 输出到\0为止 puts(str); // 输出并换行 // 注意事项 char s[5]; cin >> s; // 危险!可能缓冲区溢出 cin.getline(s, 5); // 安全,限制输入长度
// 手动计算长度 char s[] = "Hello"; int len = 0; while(s[len] != '\0') len++; // 使用循环遍历 for(int i = 0; s[i]; i++){ // s[i]为0时停止 // 处理s[i] } // 常见错误 char s1[] = {'H','i'}; // 没有\0,长度计算不准确 char s2[5] = "Hello"; // 数组越界
查找单个字符:
char s[] = "Hello"; char target = 'l'; // 查找第一次出现 int pos = -1; for(int i = 0; s[i]; i++){ if(s[i] == target){ pos = i; break; } } // 查找最后一次出现 pos = -1; for(int i = 0; s[i]; i++){ if(s[i] == target){ pos = i; } }
统计出现次数:
char s[] = "Hello"; char target = 'l'; int count = 0; for(int i = 0; s[i]; i++){ if(s[i] == target){ count++; } } // 使用数组统计所有字符 int freq[128] = {0}; // ASCII字符频率 for(int i = 0; s[i]; i++){ freq[s[i]]++; }
// 替换单个字符 char s[] = "Hello"; char old_ch = 'l'; char new_ch = 'w'; for(int i = 0; s[i]; i++){ if(s[i] == old_ch){ s[i] = new_ch; } } // 结果: "Hewwo" // 大小写转换 for(int i = 0; s[i]; i++){ if(s[i] >= 'a' && s[i] <= 'z'){ s[i] = s[i] - 32; // 转大写 } } // 结果: "HELLO"
// 比较两个字符串 char s1[] = "Hello"; char s2[] = "Hello"; bool isEqual = true; for(int i = 0; s1[i] || s2[i]; i++){ if(s1[i] != s2[i]){ isEqual = false; break; } } // 字典序比较 int compare(char s1[], char s2[]){ int i = 0; while(s1[i] && s2[i]){ if(s1[i] != s2[i]){ return s1[i] - s2[i]; } i++; } return s1[i] - s2[i]; // 处理不等长情况 }
// 判断回文串 bool isPalindrome(char s[]){ int len = 0; while(s[len]) len++; // 计算长度 for(int i = 0; i < len/2; i++){ if(s[i] != s[len-1-i]){ return false; } } return true; } // 忽略大小写的回文判断 bool isPalindromeIgnoreCase(char s[]){ int len = 0; while(s[len]) len++; for(int i = 0; i < len/2; i++){ char c1 = s[i], c2 = s[len-1-i]; // 转换为小写比较 if(c1 >= 'A' && c1 <= 'Z') c1 += 32; if(c2 >= 'A' && c2 <= 'Z') c2 += 32; if(c1 != c2) return false; } return true; }
// 查找子串 int findSubstring(char s[], char sub[]){ int i = 0, j; while(s[i]){ j = 0; while(sub[j] && s[i+j] == sub[j]){ j++; } if(!sub[j]) return i; // 找到子串 i++; } return -1; // 未找到 } // 示例使用 char text[] = "Hello World"; char pattern[] = "World"; int pos = findSubstring(text, pattern); // pos = 6
1. 字符统计 (HDU 2017)
char s[100]; cin.getline(s, 100); int count = 0; for(int i = 0; s[i]; i++){ if(s[i] >= '0' && s[i] <= '9'){ count++; } } cout << count << endl;
2. 大小写转换 (HDU 2026)
char s[100]; cin.getline(s, 100); if(s[0] >= 'a' && s[0] <= 'z'){ s[0] -= 32; } for(int i = 1; s[i]; i++){ if(s[i-1] == ' ' && s[i] >= 'a' && s[i] <= 'z'){ s[i] -= 32; } } cout << s << endl;
1. 回文串 (POJ 1488)
char s[1000], t[1000]; int len = 0; cin.getline(s, 1000); // 预处理:去除空格,转小写 for(int i = 0; s[i]; i++){ if(s[i] != ' '){ if(s[i] >= 'A' && s[i] <= 'Z'){ t[len++] = s[i] + 32; } else { t[len++] = s[i]; } } } t[len] = '\0';
2. 字符串匹配 (HDU 2087)
char s[1001], p[1001]; cin >> s >> p; int count = 0, pos = 0; while(s[pos]){ bool found = true; for(int i = 0; p[i]; i++){ if(!s[pos+i] || s[pos+i] != p[i]){ found = false; break; } } if(found){ count++; int i = 0; while(p[i]) i++; pos += i; } else { pos++; } } cout << count << endl;
USACO 2016 Dec Bronze
CSP-J 2019
POJ 3461
感谢学习! 如有疑问,请联系: judal@xmaker.org 魅客科创中心
这个讲义使用了Awesome Marp主题的蓝色风格。 内容针对竞赛选手设计,注重基础概念和实际应用。